summaryrefslogtreecommitdiffstats
path: root/idrop-web/grails-app/controllers/org/irods/mydrop/controller/LoginController.groovy
blob: 6cbe5e6a028b98e09b5f18e27e18f8d57c8d34bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package org.irods.mydrop.controller

import org.irods.jargon.core.connection.IRODSAccount
import org.irods.jargon.core.connection.auth.AuthResponse
import org.irods.jargon.core.exception.JargonException
import org.irods.jargon.core.pub.IRODSAccessObjectFactory
import org.irods.jargon.core.pub.ResourceAO
import org.irods.jargon.core.pub.UserAO
import org.irods.mydrop.service.ViewStateService

class LoginController {

	IRODSAccessObjectFactory irodsAccessObjectFactory
	IRODSAccount irodsAccount
	ViewStateService viewStateService
	
	def afterInterceptor = {
		//log.debug("closing the session")
		//irodsAccessObjectFactory.closeSession()
	}


	//static allowedMethods = [authenticate:'POST']

	def beforeInterceptor = [action:this.&auth, except:[
			'login',
			'index',
			'authenticate'
		]]

	def auth() {
		if(!session["SPRING_SECURITY_CONTEXT"]) {
			redirect(controller:"login", action:"login")
			return false
		}
		irodsAccount = session["SPRING_SECURITY_CONTEXT"]
	}

	def login = {
		log.info "in login"
		//log.info  "params:${request.parameterMap}"
		log.info "params:${params}"
		log.info("config is:${grailsApplication.config}")
		def presetHost = grailsApplication.config.idrop.config.preset.host
		def presetPort = grailsApplication.config.idrop.config.preset.port
		def presetZone = grailsApplication.config.idrop.config.preset.zone
		def presetResource = grailsApplication.config.idrop.config.preset.resource
		def presetAuthScheme = grailsApplication.config.idrop.config.preset.authScheme

		response.setHeader("apptimeout","apptimeout")

		LoginCommand loginCommand = new LoginCommand()

		if (presetHost) {
			loginCommand.host = presetHost
			loginCommand.usePresets = true
		} else {
			loginCommand.host = "elder10.man.poznan.pl"
			loginCommand.usePresets = false
		}

		if (presetPort) {
			loginCommand.port = Integer.parseInt(presetPort)
		} else {
			loginCommand.port = 1247
		}

		if (presetZone) {
			loginCommand.zone = presetZone
		} else {
			loginCommand.zone = "testZone"
		}

		if (presetResource) {
			loginCommand.defaultStorageResource = presetResource
		}

		if (presetAuthScheme) {
			log.info("preset auth scheme is:${presetAuthScheme}")
			loginCommand.authMethod = presetAuthScheme
		}
		
		render(view:"login", model:[loginCommand:loginCommand])

	}

	def index ={ redirect(action: "login") }

	def authenticate(LoginCommand loginCommand) {

		log.info "cmd: ${loginCommand}"

		/**
		 * If there is an error send back the view for redisplay with error messages
		 */
		if (!loginCommand.validate()) {
			log.info("errors in page, returning with error info:${loginCommand}")
			flash.error =  message(code:"error.data.error")
			render(view:"login",  model:[loginCommand:loginCommand])
			return
		}

		log.info("edits pass")

		def resource =  loginCommand.defaultStorageResource ? loginCommand.defaultStorageResource : ""
		def userName =  loginCommand.user ? loginCommand.user : ""
		def password =  loginCommand.password ? loginCommand.password : ""

		log.info("default storage resource: ${resource}")

		boolean success = true
		IRODSAccount irodsAccount

		if (loginCommand.useGuestLogin) {

			log.info("generate a guest login")
			irodsAccount = IRODSAccount.instanceForAnonymous(loginCommand.host,
					loginCommand.port,
					"",
					loginCommand.zone,
					resource)

		} else {

			log.info("normal login mode")
			if (userName == "") {
				loginCommand.errors.reject("error.auth.invalid.user","Invalid user or password")
				render(view:"login", model:[loginCommand:loginCommand])
				return
			}

			if (password == "" ) {
				loginCommand.errors.reject("error.auth.invalid.user","Invalid user or password")
				render(view:"login", model:[loginCommand:loginCommand])
				return
			}

			irodsAccount = IRODSAccount.instance(
					loginCommand.host,
					loginCommand.port,
					userName,
					password,
					"",
					loginCommand.zone,
					resource)
		}

		log.info("login mode: ${loginCommand.authMethod}")

		if (loginCommand.authMethod == "Standard") {
			irodsAccount.authenticationScheme = IRODSAccount.AuthScheme.STANDARD
		} else if (loginCommand.authMethod == "PAM") {
			irodsAccount.authenticationScheme = IRODSAccount.AuthScheme.PAM
		} else {
			log.error("authentication scheme invalid", e)
			response.sendError(500,e.message)
			return
		}

		log.info("built irodsAccount:${irodsAccount}")
		AuthResponse authResponse
		try {
			authResponse = irodsAccessObjectFactory.authenticateIRODSAccount(irodsAccount)
			viewStateService.clearViewState()
		} catch (JargonException e) {
			log.error("unable to authenticate, JargonException", e)

			if (e.getCause() == null) {
				if (e.getMessage().indexOf("-826000") > -1) {
					log.warn("invalid user/password")
					loginCommand.errors.reject("error.auth.invalid.user","Invalid user or password")
					success = false
				} else {
					log.error("authentication service exception", e)

					loginCommand.errors.reject("error.auth.invalid.user","Unable to authenticate")
					success = false
				}
			} else if (e.getCause() instanceof UnknownHostException) {
				log.warn("cause is invalid host")
				loginCommand.errors.reject("error.auth.invalid.host","Unknown host")
				success = false
			} else if (e.getCause().getMessage().indexOf("refused") > -1) {
				log.error("cause is refused or invalid port")
				loginCommand.errors.reject("error.auth.connection.refused","Connection refused")
				success = false
			} else {
				log.error("authentication service exception", e)
				response.sendError(500,e.message)
				return
			}
		}

		if (!success) {
			log.warn("unsuccessful, render the login again")
			render(view:"login", model:[loginCommand:loginCommand])
			return
		}
		session["SPRING_SECURITY_CONTEXT"] = authResponse.authenticatedIRODSAccount
		redirect(controller:"home")
	}

	def logout = {
		session["SPRING_SECURITY_CONTEXT"] = null
		session.invalidate()
		redirect(action:"login")
	}
	
	
	/**
	 * FIXME: deprecated
	 * Show information about the current user/host
	 */
	def showLoginBar = {
		log.info("showLoginBar()")
		ResourceAO resourceAO = irodsAccessObjectFactory.getResourceAO(irodsAccount)
		List<String> resources = new ArrayList<String>()
		resources.add("")
		resources.addAll(resourceAO.listResourceAndResourceGroupNames())
		render(view:"defaultStorageResource", model:[irodsAccount:irodsAccount, resources:resources])
	}
	
	/**
	 * Show a dialog to set the default storage resource
	 */
	def defaultResource = {
		log.info("showLoginBar()")
		ResourceAO resourceAO = irodsAccessObjectFactory.getResourceAO(irodsAccount)
		List<String> resources = new ArrayList<String>()
		resources.add("")
		resources.addAll(resourceAO.listResourceAndResourceGroupNames())
		render(view:"defaultStorageResource", model:[irodsAccount:irodsAccount, resources:resources])
	}
	
	/**
	 * Show the password change dialog
	 * @return
	 */
	def changePasswordForm() {
		PasswordCommand cmd = new PasswordCommand()
		render (view:"passwordChange", model:[irodsAccount:irodsAccount, password:cmd])
	}
	
	/**
	 * process a password change
	 * @return
	 */
	def changePassword(PasswordCommand cmd) {
		log.info "passwordChange()"
		log.info "cmd: ${cmd}"

		/**
		 * If there is an error send back the view for redisplay with error messages
		 */
		if (!cmd.validate()) {
			log.info("errors in page, returning with error info:${cmd}")
			flash.error =  message(code:"error.data.error")
			render (view:"passwordChange", model:[irodsAccount:irodsAccount, password:cmd])
			return
		}

		log.info("edits pass")
		
		UserAO userAO = irodsAccessObjectFactory.getUserAO(irodsAccount)
		userAO.changeAUserPasswordByThatUser(irodsAccount.userName, irodsAccount.password, cmd.password)
		irodsAccount.password = cmd.password
		log.info("password changed, fixed account in session")
		flash.message = message(code:"message.password.updated")

		render (view:"passwordChange", model:[irodsAccount:irodsAccount, password:cmd])

	}

	
}
class LoginCommand {
	boolean useGuestLogin
	boolean usePresets
	String user
	String password
	String host
	String zone
	int port
	String defaultStorageResource
	String authMethod

	static constraints = {
		host(blank:false)
		zone(blank:false)
		port( min:1, max:Integer.MAX_VALUE)
		authMethod(blank:false)
	}
}

class PasswordCommand {
	
		String password
		String confirmPassword
	
		static constraints = {
			password(blank:false)
			confirmPassword  validator: {
				val, obj ->
				if (!val) return ['error.confirm.password.missing']
				 if (val != obj.password) return['error.passwords.dont.match']
			}
		}
	}