|
| 1 | +/* Copyright 2006-2010 the original author or authors. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +import org.codehaus.groovy.grails.plugins.springsecurity.SecurityFilterPosition |
| 16 | +import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils |
| 17 | + |
| 18 | +import org.springframework.security.access.ConfigAttribute |
| 19 | +import org.springframework.security.access.SecurityConfig |
| 20 | +import org.springframework.security.oauth.common.signature.CoreOAuthSignatureMethodFactory |
| 21 | +import org.springframework.security.oauth.common.signature.HMAC_SHA1SignatureMethod |
| 22 | +import org.springframework.security.oauth.common.signature.SharedConsumerSecret |
| 23 | +import org.springframework.security.oauth.consumer.BaseProtectedResourceDetails |
| 24 | +import org.springframework.security.oauth.consumer.CoreOAuthConsumerSupport |
| 25 | +import org.springframework.security.oauth.consumer.InMemoryProtectedResourceDetailsService |
| 26 | +import org.springframework.security.oauth.consumer.OAuthConsumerProcessingFilter |
| 27 | +import org.springframework.security.oauth.consumer.net.DefaultOAuthURLStreamHandlerFactory |
| 28 | +import org.springframework.security.oauth.consumer.nonce.UUIDNonceFactory |
| 29 | +import org.springframework.security.oauth.consumer.token.HttpSessionBasedTokenServicesFactory |
| 30 | +import org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource |
| 31 | +import org.springframework.security.web.access.intercept.RequestKey |
| 32 | +import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint |
| 33 | +import org.springframework.security.web.util.AntUrlPathMatcher |
| 34 | +import org.springframework.security.web.util.RegexUrlPathMatcher |
| 35 | +import org.springframework.util.StringUtils |
| 36 | + |
| 37 | +class SpringSecurityOauthConsumerGrailsPlugin { |
| 38 | + |
| 39 | + String version = '0.1' |
| 40 | + String grailsVersion = '1.2.2 > *' |
| 41 | + Map dependsOn = ['springSecurityCore': '0.4 > *'] |
| 42 | + |
| 43 | + List pluginExcludes = [ |
| 44 | + 'docs/**', |
| 45 | + 'src/docs/**' |
| 46 | + ] |
| 47 | + |
| 48 | + String author = 'Burt Beckwith' |
| 49 | + String authorEmail = '[email protected]' |
| 50 | + String title = 'OAuth Consumer support for the Spring Security plugin.' |
| 51 | + String description = 'OAuth Consumer support for the Spring Security plugin.' |
| 52 | + |
| 53 | + String documentation = 'http://grails.org/plugin/spring-security-oauth-consumer' |
| 54 | + |
| 55 | + def doWithSpring = { |
| 56 | + |
| 57 | + def conf = SpringSecurityUtils.securityConfig |
| 58 | + if (!conf || !conf.active) { |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + println 'Configuring Spring Security OAuth Consumer ...' |
| 63 | + |
| 64 | + SpringSecurityUtils.loadSecondaryConfig 'DefaultOAuthConsumerSecurityConfig' |
| 65 | + // have to get again after overlaying DefaultOAuthConsumerSecurityConfig |
| 66 | + conf = SpringSecurityUtils.securityConfig |
| 67 | + |
| 68 | + SpringSecurityUtils.registerFilter 'oauthConsumerFilter', |
| 69 | + SecurityFilterPosition.LAST.order - 1 |
| 70 | + |
| 71 | + def resources = parseResources(conf) |
| 72 | + oauthProtectedResourceDetailsService(InMemoryProtectedResourceDetailsService) { |
| 73 | + resourceDetailsStore = resources |
| 74 | + } |
| 75 | + |
| 76 | + oauthSignatureMethodFactory(CoreOAuthSignatureMethodFactory) { |
| 77 | + supportPlainText = conf.oauthConsumer.signature.supportPlainText // false |
| 78 | + supportHMAC_SHA1 = conf.oauthConsumer.signature.supportHMAC_SHA1 // true |
| 79 | + supportRSA_SHA1 = conf.oauthConsumer.signature.supportRSA_SHA1 // true |
| 80 | + } |
| 81 | + |
| 82 | + oauthNonceFactory(UUIDNonceFactory) |
| 83 | + |
| 84 | + oauthStreamHandlerFactory(DefaultOAuthURLStreamHandlerFactory) |
| 85 | + |
| 86 | + oauthProxySelector(ProxySelector) { bean -> |
| 87 | + bean.factoryMethod = 'getDefault' |
| 88 | + } |
| 89 | + |
| 90 | + oauthConsumerSupport(CoreOAuthConsumerSupport) { |
| 91 | + connectionTimeout = conf.oauthConsumer.protectedResource.connectionTimeout // 1000 * 60 |
| 92 | + readTimeout = conf.oauthConsumer.protectedResource.readTimeout // 1000 * 60 |
| 93 | + signatureFactory = ref('oauthSignatureMethodFactory') |
| 94 | + protectedResourceDetailsService = ref('oauthProtectedResourceDetailsService') |
| 95 | + nonceFactory = ref('oauthNonceFactory') |
| 96 | + streamHandlerFactory = ref('oauthStreamHandlerFactory') |
| 97 | + proxySelector = ref('oauthProxySelector') |
| 98 | + } |
| 99 | + |
| 100 | + oauthTokenServicesFactory(HttpSessionBasedTokenServicesFactory) |
| 101 | + |
| 102 | + oauthFailureEntryPoint(LoginUrlAuthenticationEntryPoint) { |
| 103 | + forceHttps = conf.auth.forceHttps // false |
| 104 | + useForward = conf.auth.useForward // false |
| 105 | + loginFormUrl = conf.oauthConsumer.failurePage |
| 106 | + portMapper = ref('portMapper') |
| 107 | + portResolver = ref('portResolver') |
| 108 | + } |
| 109 | + |
| 110 | + String pathType = conf.oauthConsumer.ods.pathType // 'ant' |
| 111 | + def urlMatcher = 'ant'.equals(pathType) ? new AntUrlPathMatcher() : new RegexUrlPathMatcher() |
| 112 | + urlMatcher.requiresLowerCaseUrl = conf.oauthConsumer.ods.lowercaseComparisons // true |
| 113 | + |
| 114 | + LinkedHashMap<RequestKey, Collection<ConfigAttribute>> invocationDefinitionMap = [:] |
| 115 | + for (Map<String, String> map : conf.oauthConsumer.ods.urls) { |
| 116 | + String path = map.pattern |
| 117 | + if (conf.oauthConsumer.ods.lowercaseComparisons) { |
| 118 | + path = path.toLowerCase() |
| 119 | + } |
| 120 | + if (map.resources) { |
| 121 | + invocationDefinitionMap[new RequestKey(path, map.httpMethod ?: null)] = SecurityConfig.createList( |
| 122 | + StringUtils.commaDelimitedListToStringArray(map.resources)) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + oauthObjectDefinitionSource(DefaultFilterInvocationSecurityMetadataSource, urlMatcher, invocationDefinitionMap) |
| 127 | + |
| 128 | + oauthConsumerFilter(OAuthConsumerProcessingFilter) { |
| 129 | + protectedResourceDetailsService = ref('oauthProtectedResourceDetailsService') |
| 130 | + OAuthFailureEntryPoint = ref('oauthFailureEntryPoint') |
| 131 | + tokenServicesFactory = ref('oauthTokenServicesFactory') |
| 132 | + objectDefinitionSource = ref('oauthObjectDefinitionSource') |
| 133 | + consumerSupport = ref('oauthConsumerSupport') |
| 134 | + portResolver = ref('portResolver') |
| 135 | + requireAuthenticated = conf.oauthConsumer.consumerFilter.requireAuthenticated // true |
| 136 | + accessTokensRequestAttribute = conf.oauthConsumer.consumerFilter.accessTokensRequestAttribute // 'OAUTH_ACCESS_TOKENS' |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private Map<String, BaseProtectedResourceDetails> parseResources(conf) { |
| 141 | + Map<String, BaseProtectedResourceDetails> resources = new TreeMap<String, BaseProtectedResourceDetails>() |
| 142 | + |
| 143 | + for (Map<String, Object> resourceDef in conf.oauthConsumer.resourceDefs) { |
| 144 | + BaseProtectedResourceDetails resource = new BaseProtectedResourceDetails() |
| 145 | + |
| 146 | + setIfString 'id', resourceDef, resource, 'A resource id' |
| 147 | + setIfString 'consumerKey', resourceDef, resource, 'A consumer key' |
| 148 | + setIfString 'requestTokenURL', resourceDef, resource, 'A request token URL' |
| 149 | + setIfString 'accessTokenURL', resourceDef, resource, 'An access token URL' |
| 150 | + setIfString 'accessTokenHttpMethod', resourceDef, resource |
| 151 | + setIfString 'userAuthorizationURL', resourceDef, resource, 'A user authorization URL' |
| 152 | + setIfString 'authorizationHeaderRealm' , resourceDef, resource |
| 153 | + setIfBoolean 'acceptsAuthorizationHeader', resourceDef, resource |
| 154 | + setIfBoolean 'use10a', resourceDef, resource |
| 155 | + |
| 156 | + def secret = resourceDef.secret |
| 157 | + if (secret instanceof String && secret) { |
| 158 | + resource.sharedSecret = new SharedConsumerSecret(secret) |
| 159 | + } |
| 160 | + else { |
| 161 | + error "A shared secret must be supplied with the definition of a resource: $resourceDef" |
| 162 | + } |
| 163 | + |
| 164 | + def signatureMethod = resourceDef.signatureMethod |
| 165 | + if (!(signatureMethod instanceof String && signatureMethod)) { |
| 166 | + signatureMethod = HMAC_SHA1SignatureMethod.SIGNATURE_NAME |
| 167 | + } |
| 168 | + resource.signatureMethod = signatureMethod |
| 169 | + |
| 170 | + def additionalParameters = resourceDef.addtionalParameters |
| 171 | + if (additionalParameters instanceof Map && additionalParameters) { |
| 172 | + resource.additionalParameters = additionalParameters |
| 173 | + } |
| 174 | + |
| 175 | + def additionalRequestHeaders = resourceDef.additionalRequestHeaders |
| 176 | + if (additionalRequestHeaders instanceof Map && additionalRequestHeaders) { |
| 177 | + resource.additionalRequestHeaders = additionalRequestHeaders |
| 178 | + } |
| 179 | + |
| 180 | + resources[resourceDef.id] = resource |
| 181 | + } |
| 182 | + |
| 183 | + resources |
| 184 | + } |
| 185 | + |
| 186 | + private void setIfString(String attrName, Map<String, Object> resourceDef, |
| 187 | + BaseProtectedResourceDetails resource, String errorPrefix = null) { |
| 188 | + |
| 189 | + def value = resourceDef."$attrName" |
| 190 | + if (value instanceof String && value) { |
| 191 | + resource."$attrName" = value |
| 192 | + return |
| 193 | + } |
| 194 | + |
| 195 | + if (errorPrefix) { |
| 196 | + error "$errorPrefix must be supplied with the definition of a protected resource: $resourceDef" |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private void setIfBoolean(String attrName, Map<String, Object> resourceDef, BaseProtectedResourceDetails resource) { |
| 201 | + def value = resourceDef."$attrName" |
| 202 | + if (value instanceof Boolean) { |
| 203 | + resource."$attrName" = value |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private void error(String message) { |
| 208 | + // OAuth parser throws BeanDefinitionParsingException but Problem/Location aren't as necessary in Grails |
| 209 | + throw new RuntimeException(message) |
| 210 | + } |
| 211 | +} |
0 commit comments