forked from splix/grails-spring-security-facebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpringSecurityFacebookGrailsPlugin.groovy
287 lines (253 loc) · 13.3 KB
/
SpringSecurityFacebookGrailsPlugin.groovy
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
/* Copyright 2006-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.the6hours.grails.springsecurity.facebook.DomainsRelation
import com.the6hours.grails.springsecurity.facebook.FacebookAuthProvider
import com.the6hours.grails.springsecurity.facebook.FacebookAuthJsonFilter
import com.the6hours.grails.springsecurity.facebook.FacebookAuthCookieTransparentFilter
import com.the6hours.grails.springsecurity.facebook.FacebookAuthUtils
import com.the6hours.grails.springsecurity.facebook.JsonAuthenticationHandler
import grails.util.Environment
import grails.util.Metadata
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
import com.the6hours.grails.springsecurity.facebook.FacebookAuthCookieLogoutHandler
import com.the6hours.grails.springsecurity.facebook.DefaultFacebookAuthDao
import com.the6hours.grails.springsecurity.facebook.FacebookAuthCookieDirectFilter
import com.the6hours.grails.springsecurity.facebook.FacebookAuthRedirectFilter
class SpringSecurityFacebookGrailsPlugin {
String version = '0.14.5'
String grailsVersion = '2.0.0 > *'
Map dependsOn = ['springSecurityCore': '1.2.7.2 > *']
def license = 'APACHE'
def developers = [
//extra developers
]
def issueManagement = [system: "GitHub", url: "https://github.com/splix/grails-spring-security-facebook/issues"]
def scm = [url: "http://github.com/splix/grails-spring-security-facebook"]
String author = 'Igor Artamonov'
String authorEmail = '[email protected]'
String title = 'Facebook Authentication for Spring Security'
String description = 'Facebook Authentication for Spring Security Core plugin'
String documentation = "http://splix.github.io/grails-spring-security-facebook/"
def organization = [ name: "The 6 Hours", url: "http://the6hours.com/" ]
def observe = ["springSecurityCore"]
String _facebookDaoName
def doWithSpring = {
if (Environment.current == Environment.TEST) {
if (Metadata.getCurrent().getApplicationName() == 'spring-security-facebook') {
println "Test mode. Skipping initial plugin initialization"
return
} else {
log.debug("Run in test mode")
}
}
def conf = SpringSecurityUtils.securityConfig
if (!conf) {
println 'ERROR: There is no Spring Security configuration'
println 'ERROR: Stop configuring Spring Security Facebook'
return
}
if (!this.hasProperty('log')) {
println 'WARN: No such property: log for class: SpringSecurityFacebookGrailsPlugin'
println 'WARN: Running from a unit test?'
println 'WARN: Introducing a log property for plugin'
this.metaClass.log = org.apache.commons.logging.LogFactory.getLog(SpringSecurityFacebookGrailsPlugin)
}
println 'Configuring Spring Security Facebook ...'
SpringSecurityUtils.loadSecondaryConfig 'DefaultFacebookSecurityConfig'
// have to get again after overlaying DefaultFacebookecurityConfig
conf = SpringSecurityUtils.securityConfig
_facebookDaoName = conf?.facebook?.bean?.dao ?: null
if (_facebookDaoName == null) {
_facebookDaoName = 'facebookAuthDao'
String _domainsRelation = getConfigValue(conf, 'facebook.domain.relation')
String _appUserConnectionPropertyName = getConfigValue(conf, 'facebook.domain.appUserConnectionPropertyName', 'facebook.domain.connectionPropertyName')
List<String> _roles = getAsStringList(conf.facebook.autoCreate.roles, 'grails.plugins.springsecurity.facebook.autoCreate.roles')
facebookAuthDao(DefaultFacebookAuthDao) {
domainClassName = conf.facebook.domain.classname
appUserConnectionPropertyName = _appUserConnectionPropertyName
userDomainClassName = conf.userLookup.userDomainClassName
rolesPropertyName = conf.userLookup.authoritiesPropertyName
coreUserDetailsService = ref('userDetailsService')
if (_domainsRelation) {
domainsRelation = DomainsRelation.getFrom(_domainsRelation)
}
defaultRoleNames = _roles
}
} else {
log.info("Using provided Facebook Auth DAO bean: $_facebookDaoName")
}
List<String> _filterTypes = parseFilterTypes(conf)
List<String> _requiredPermissions = getAsStringList(conf.facebook.permissions, 'Required Permissions', 'facebook.permissions')
facebookAuthUtils(FacebookAuthUtils) {
apiKey = conf.facebook.apiKey
secret = conf.facebook.secret
applicationId = conf.facebook.appId
filterTypes = _filterTypes
requiredPermissions = _requiredPermissions
}
SpringSecurityUtils.registerProvider 'facebookAuthProvider'
boolean _createNew = getConfigValue(conf, 'facebook.autoCreate.enabled') ? conf.facebook.autoCreate.enabled as Boolean : false
facebookAuthProvider(FacebookAuthProvider) {
facebookAuthDao = ref(_facebookDaoName)
facebookAuthUtils = ref('facebookAuthUtils')
createNew = _createNew
}
addFilters(conf, delegate, _filterTypes)
println '... finished configuring Spring Security Facebook'
}
private List<String> parseFilterTypes(def conf) {
def typesRaw = conf.facebook.filter.types
List<String> types = null
if (!typesRaw) {
log.warn("Value for 'grails.plugins.springsecurity.facebook.filter.types' is empty")
typesRaw = conf.facebook.filter.type
}
String defaultType = 'transparent'
List validTypes = ['transparent', 'cookieDirect', 'redirect', 'json']
if (!typesRaw) {
log.error("Invalid Facebook Authentication filters configuration: '$typesRaw'. Should be used on of: $validTypes. Current value will be ignored, and type '$defaultType' will be used instead.")
types = [defaultType]
} else if (typesRaw instanceof Collection) {
types = typesRaw.collect { it.toString() }.findAll { it in validTypes }
} else if (typesRaw instanceof String) {
types = typesRaw.split(',').collect { it.trim() }.findAll { it in validTypes }
} else {
log.error("Invalid Facebook Authentication filters configuration, invalid value type: '${typesRaw.getClass()}'. Filter typer should be defined as a Collection or String (comma separated, if you need few filters). Type '$defaultType' will be used instead.")
types = [defaultType]
}
if (!types || types.empty) {
log.error("Facebook Authentication filter is not configured. Should be used one of: $validTypes. So '$defaultType' will be used by default.")
log.error("To configure Facebook Authentication filters you should add to Config.groovy:")
log.error("grails.plugins.springsecurity.facebook.filter.types='transparent'")
log.error("or")
log.error("grails.plugins.springsecurity.facebook.filter.types='redirect,transparent,cookieDirect'")
types = [defaultType]
}
return types
}
private void addFilters(def conf, def delegate, def types) {
int basePosition = conf.facebook.filter.position
addFilter.delegate = delegate
types.eachWithIndex { name, idx ->
addFilter(conf, name, basePosition + 1 + idx)
}
}
private addFilter = { def conf, String name, int position ->
if (name == 'transparent') {
SpringSecurityUtils.registerFilter 'facebookAuthCookieTransparentFilter', position
facebookAuthCookieTransparentFilter(FacebookAuthCookieTransparentFilter) {
authenticationManager = ref('authenticationManager')
facebookAuthUtils = ref('facebookAuthUtils')
logoutUrl = conf.logout.filterProcessesUrl
forceLoginParameter = conf.facebook.filter.forceLoginParameter
}
facebookAuthCookieLogout(FacebookAuthCookieLogoutHandler) {
facebookAuthUtils = ref('facebookAuthUtils')
facebookAuthDao = ref(_facebookDaoName)
}
SpringSecurityUtils.registerLogoutHandler('facebookAuthCookieLogout')
} else if (name == 'cookieDirect') {
String _successHandler = getConfigValue(conf, 'facebook.filter.cookieDirect.successHandler')
String _failureHandler = getConfigValue(conf, 'facebook.filter.cookieDirect.failureHandler')
String url = getConfigValue(conf, 'facebook.filter.cookieDirect.processUrl', 'facebook.filter.processUrl')
SpringSecurityUtils.registerFilter 'facebookAuthCookieDirectFilter', position
facebookAuthCookieDirectFilter(FacebookAuthCookieDirectFilter, url) {
authenticationManager = ref('authenticationManager')
facebookAuthUtils = ref('facebookAuthUtils')
if (_successHandler) {
authenticationSuccessHandler = ref(_successHandler)
}
if (_failureHandler) {
authenticationFailureHandler = ref(_failureHandler)
}
}
} else if (name == 'redirect') {
SpringSecurityUtils.registerFilter 'facebookAuthRedirectFilter', position
String successHandler = getConfigValue(conf, 'facebook.filter.redirect.successHandler')
String failureHandler = getConfigValue(conf, 'facebook.filter.redirect.failureHandler')
String _url = getConfigValue(conf, 'facebook.filter.redirect.processUrl', 'facebook.filter.processUrl')
String _redirectFromUrl = getConfigValue(conf, 'facebook.filter.redirect.redirectFromUrl', 'facebook.filter.redirectFromUrl')
facebookAuthRedirectFilter(FacebookAuthRedirectFilter, _url) {
authenticationManager = ref('authenticationManager')
facebookAuthUtils = ref('facebookAuthUtils')
redirectFromUrl = _redirectFromUrl
linkGenerator = ref('grailsLinkGenerator')
if (successHandler) {
authenticationSuccessHandler = ref(successHandler)
}
if (failureHandler) {
authenticationFailureHandler = ref(failureHandler)
}
}
} else if (name == 'json') {
SpringSecurityUtils.registerFilter 'facebookAuthJsonFilter', position
String _url = conf.facebook.filter.json.processUrl
boolean _jsonp = '_jsonp'.equalsIgnoreCase(conf.facebook.filter.json.type)
facebookJsonAuthenticationHandler(JsonAuthenticationHandler) {
useJsonp = _jsonp
}
List<String> _methods = getAsStringList(conf.facebook.filter.json.methods, '**.facebook.filter.json.type')
_methods = _methods ? _methods*.toUpperCase() : ['POST']
if (_jsonp) {
_methods = ['GET']
}
facebookAuthJsonFilter(FacebookAuthJsonFilter, _url) {
methods = _methods
authenticationManager = ref('authenticationManager')
facebookAuthUtils = ref('facebookAuthUtils')
authenticationSuccessHandler = ref('facebookJsonAuthenticationHandler')
authenticationFailureHandler = ref('facebookJsonAuthenticationHandler')
}
} else {
log.error("Invalid filter type: $name")
}
}
def doWithApplicationContext = { ctx ->
}
def onConfigChange = { event ->
println("Config change")
SpringSecurityUtils.resetSecurityConfig()
}
private Object getConfigValue(def conf, String... values) {
conf = conf.flatten()
String key = values.find {
if (!conf.containsKey(it)) {
return false
}
def val = conf.get(it)
if (val == null || val.toString() == '{}') {
return false
}
return true
}
if (key) {
return conf.get(key)
}
return null
}
private List<String> getAsStringList(def conf, String paramHumanName, String paramName = '???') {
def raw = conf
if (raw == null) {
log.error("Invalid $paramHumanName filters configuration: '$raw'")
} else if (raw instanceof Collection) {
return raw.collect { it.toString() }
} else if (raw instanceof String) {
return raw.split(',').collect { it.trim() }
} else {
log.error("Invalid $paramHumanName filters configuration, invalid value type: '${raw.getClass()}'. Value should be defined as a Collection or String (comma separated)")
}
return null
}
}