-
Notifications
You must be signed in to change notification settings - Fork 2
/
SpringSecurityOauth2ProviderGrailsPlugin.groovy
187 lines (154 loc) · 7.4 KB
/
SpringSecurityOauth2ProviderGrailsPlugin.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
/* 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 org.apache.log4j.Logger;
import org.codehaus.groovy.grails.plugins.springsecurity.SecurityFilterPosition
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
import org.springframework.security.oauth2.provider.code.InMemoryAuthorizationCodeServices
import org.springframework.security.oauth2.provider.BaseClientDetails
import org.springframework.security.oauth2.provider.InMemoryClientDetailsService
import org.springframework.security.oauth2.provider.token.InMemoryTokenStore
import org.springframework.security.oauth2.provider.token.RandomValueTokenServices
import org.springframework.security.oauth2.provider.filter.OAuth2ExceptionHandlerFilter
import org.springframework.security.oauth2.provider.filter.OAuth2ProtectedResourceFilter
import grails.plugins.springsecurity.oauthprovider.SpringSecurityOAuth2ProviderUtility
class SpringSecurityOauth2ProviderGrailsPlugin {
static Logger log = Logger.getLogger('grails.app.bootstrap.BootStrap')
def version = "1.0.0.M5.1-SNAPSHOT"
String grailsVersion = '1.2.2 > *'
List pluginExcludes = [
'docs/**',
'src/docs/**',
// Domains
'test/**',
// Controllers
'grails-app/controllers/**',
'grails-app/domain/**',
'grails-app/i18n/**',
// Views
'web-app/**',
'grails-app/views/login/**',
'grails-app/views/secured/**',
'grails-app/views/index.gsp',
]
//Map dependsOn = [springSecurityCore: '1.0 > *']
def loadAfter = ["springSecurityCore"]
def license = "APACHE"
def organization = [ name:"Adaptive Computing", url:"http://adaptivecomputing.com" ]
def issueManagement = [ system:"GitHub", url:"http://github.com/adaptivecomputing/grails-spring-security-oauth2-provider/issues" ]
def scm = [ url:"http://github.com/adaptivecomputing/grails-spring-security-oauth2-provider" ]
String author = 'Brian Saville'
String authorEmail = '[email protected]'
String title = 'OAuth2 Provider support for the Spring Security plugin.'
String description = '''\
OAuth2 Provider support for the Spring Security plugin.
'''
String documentation = 'http://grails.org/plugin/spring-security-oauth2-provider'
def doWithSpring = {
def conf = SpringSecurityUtils.securityConfig
if (!conf || !conf.active) {
return
}
SpringSecurityUtils.loadSecondaryConfig 'DefaultOAuth2ProviderSecurityConfig'
// have to get again after overlaying DefaultOAuthProviderSecurityConfig
conf = SpringSecurityUtils.securityConfig
if (!conf.oauthProvider.active)
return
log.debug 'Configuring Spring Security OAuth2 provider ...'
clientDetailsService(conf.oauthProvider.clientDetailsServiceClass ?: InMemoryClientDetailsService)
tokenStore(conf.oauthProvider.tokenStoreClass ?: InMemoryTokenStore)
tokenServices(RandomValueTokenServices) {
tokenStore = ref("tokenStore")
accessTokenValiditySeconds = conf.oauthProvider.tokenServices.accessTokenValiditySeconds
refreshTokenValiditySeconds = conf.oauthProvider.tokenServices.refreshTokenValiditySeconds
reuseRefreshToken = conf.oauthProvider.tokenServices.reuseRefreshToken
supportRefreshToken = conf.oauthProvider.tokenServices.supportRefreshToken
}
authorizationCodeServices(conf.oauthProvider.authorizationCodeServicesClass ?: InMemoryAuthorizationCodeServices)
// Oauth namespace
xmlns oauth:"http://www.springframework.org/schema/security/oauth2"
oauth.'authorization-server'(
'client-details-service-ref':"clientDetailsService",
'token-services-ref':"tokenServices",
'authorization-endpoint-url':conf.oauthProvider.authorizationEndpointUrl,
'token-endpoint-url':conf.oauthProvider.tokenEndpointUrl) {
oauth.'authorization-code'(
'services-ref':"authorizationCodeServices",
'disabled':!conf.oauthProvider.grantTypes.authorizationCode,
'user-approval-page':conf.oauthProvider.userApprovalEndpointUrl,
'approval-parameter-name':conf.oauthProvider.authorizationCode.approvalParameterName)
oauth.'implicit'(
'disabled':!conf.oauthProvider.grantTypes.implicit
)
oauth.'refresh-token'(
'disabled':!conf.oauthProvider.grantTypes.refreshToken
)
oauth.'client-credentials'(
'disabled':!conf.oauthProvider.grantTypes.clientCredentials
)
oauth.'password'(
'authentication-manager-ref':'authenticationManager',
'disabled':!conf.oauthProvider.grantTypes.password
)
}
// Register endpoint URL filter since we define the URLs above
SpringSecurityUtils.registerFilter 'oauth2EndpointUrlFilter',
conf.oauthProvider.endpointUrlFilterPosition
oauth2ExceptionHandlerFilter(OAuth2ExceptionHandlerFilter)
SpringSecurityUtils.registerFilter 'oauth2ExceptionHandlerFilter',
conf.oauthProvider.exceptionHandlerFilterPosition
oauth2ProtectedResourceFilter(OAuth2ProtectedResourceFilter) {
tokenServices = ref("tokenServices")
}
SpringSecurityUtils.registerFilter 'oauth2ProtectedResourceFilter',
conf.oauthProvider.protectedResourceFilterPosition
log.debug "... done configured Spring Security OAuth2 provider"
}
def doWithApplicationContext = { applicationContext ->
def conf = SpringSecurityUtils.securityConfig
if (!conf || !conf.active) {
return
}
SpringSecurityUtils.loadSecondaryConfig 'DefaultOAuth2ProviderSecurityConfig'
// have to get again after overlaying DefaultOAuthProviderSecurityConfig
conf = SpringSecurityUtils.securityConfig
if (!conf.oauthProvider.active || !conf.oauthProvider.clients)
return
log.debug 'Configuring OAuth2 clients ...'
def clientDetailsService = applicationContext.getBean("clientDetailsService")
if (clientDetailsService instanceof InMemoryClientDetailsService)
SpringSecurityOAuth2ProviderUtility.registerClients(conf, clientDetailsService)
else
log.info("Client details service bean is not an in-memory implementation, ignoring client config")
log.debug '... done configuring OAuth2 clients'
}
def onConfigChange = { event ->
def conf = SpringSecurityUtils.securityConfig
if (!conf || !conf.active) {
return
}
SpringSecurityUtils.loadSecondaryConfig 'DefaultOAuth2ProviderSecurityConfig'
// have to get again after overlaying DefaultOAuthProviderSecurityConfig
conf = SpringSecurityUtils.securityConfig
if (!conf.oauthProvider.active || !conf.oauthProvider.clients)
return
log.debug 'Reconfiguring OAuth2 clients ...'
def clientDetailsService = applicationContext.getBean("clientDetailsService")
if (clientDetailsService instanceof InMemoryClientDetailsService)
SpringSecurityOAuth2ProviderUtility.registerClients(conf, clientDetailsService)
else
log.info("Client details service bean is not an in-memory implementation, ignoring config change")
log.debug '... done reconfiguring OAuth2 clients'
}
}