Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#12: yay! OAuth support made generic! \o/
- Loading branch information
Alvaro Sanchez-Mariscal
committed
Jan 15, 2014
1 parent
a8646ba
commit d1c9ce4
Showing
9 changed files
with
133 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class RestOauthUrlMappings { | ||
|
||
static mappings = { | ||
|
||
name oauth: "/oauth/${action}/${provider}"(controller: 'oauth') | ||
|
||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
grails-app/controllers/com/odobo/grails/plugin/springsecurity/rest/OauthController.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.odobo.grails.plugin.springsecurity.rest | ||
|
||
import grails.plugin.springsecurity.annotation.Secured | ||
import org.pac4j.core.context.J2EContext | ||
import org.pac4j.core.context.WebContext | ||
import org.pac4j.oauth.client.BaseOAuth20Client | ||
|
||
@Secured(['permitAll']) | ||
class OauthController { | ||
|
||
def oauthService | ||
def grailsApplication | ||
|
||
def authenticate(String provider) { | ||
BaseOAuth20Client client = oauthService.getClient(provider) | ||
WebContext context = new J2EContext(request, response) | ||
redirect url: client.getRedirectionUrl(context) | ||
} | ||
|
||
|
||
def callback(String provider) { | ||
BaseOAuth20Client client = oauthService.getClient(provider) | ||
WebContext context = new J2EContext(request, response) | ||
String tokenValue = oauthService.storeAuthentication(provider, context) | ||
redirect url: grailsApplication.config.grails.plugin.springsecurity.rest.oauth.frontendCallbackUrl.call(tokenValue) | ||
} | ||
|
||
|
||
|
||
} |
55 changes: 55 additions & 0 deletions
55
grails-app/services/com/odobo/grails/plugin/springsecurity/rest/OauthService.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.odobo.grails.plugin.springsecurity.rest | ||
|
||
import com.odobo.grails.plugin.springsecurity.rest.token.generation.TokenGenerator | ||
import com.odobo.grails.plugin.springsecurity.rest.token.storage.TokenStorageService | ||
import org.codehaus.groovy.grails.commons.GrailsApplication | ||
import org.codehaus.groovy.grails.web.mapping.LinkGenerator | ||
import org.pac4j.core.context.WebContext | ||
import org.pac4j.oauth.client.BaseOAuth20Client | ||
import org.pac4j.oauth.credentials.OAuthCredentials | ||
import org.pac4j.oauth.profile.OAuth20Profile | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.security.core.userdetails.UserDetails | ||
import org.springframework.security.core.userdetails.UserDetailsService | ||
|
||
class OauthService { | ||
|
||
TokenGenerator tokenGenerator | ||
TokenStorageService tokenStorageService | ||
UserDetailsService userDetailsService | ||
GrailsApplication grailsApplication | ||
LinkGenerator grailsLinkGenerator | ||
|
||
|
||
private BaseOAuth20Client<OAuth20Profile> getClient(String provider) { | ||
def providerConfig = grailsApplication.config.grails.plugin.springsecurity.rest.oauth."${provider}" | ||
def ClientClass = providerConfig.client | ||
|
||
BaseOAuth20Client<OAuth20Profile> client = ClientClass.newInstance(providerConfig.key, providerConfig.secret) | ||
client.callbackUrl = grailsLinkGenerator.link controller: 'oauth', action: 'callback', params: [provider: provider], mapping: 'oauth', absolute: true | ||
client.scope = providerConfig.scope | ||
return client | ||
} | ||
|
||
String storeAuthentication(String provider, WebContext context) { | ||
BaseOAuth20Client<OAuth20Profile> client = getClient(provider) | ||
OAuthCredentials credentials = client.getCredentials context | ||
OAuth20Profile profile = client.getUserProfile credentials | ||
|
||
log.debug "User's ID: ${profile.id}" | ||
|
||
String tokenValue = tokenGenerator.generateToken() | ||
log.debug "Generated token: ${tokenValue}" | ||
|
||
UserDetails userDetails = userDetailsService.loadUserByUsername profile.id | ||
|
||
tokenStorageService.storeToken(tokenValue, userDetails) | ||
|
||
Authentication authenticationResult = new RestAuthenticationToken(userDetails, userDetails.password, userDetails.authorities, tokenValue) | ||
|
||
SecurityContextHolder.context.setAuthentication(authenticationResult) | ||
|
||
return tokenValue | ||
} | ||
} |
55 changes: 0 additions & 55 deletions
55
src/groovy/com/odobo/grails/plugin/springsecurity/rest/oauth/OauthFilter.groovy
This file was deleted.
Oops, something went wrong.
88 changes: 0 additions & 88 deletions
88
src/groovy/com/odobo/grails/plugin/springsecurity/rest/oauth/OauthTokenFilter.groovy
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
test/unit/com/odobo/grails/plugin/springsecurity/rest/OauthControllerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.odobo.grails.plugin.springsecurity.rest | ||
|
||
import grails.test.mixin.TestFor | ||
import spock.lang.Specification | ||
|
||
/** | ||
* See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions | ||
*/ | ||
@TestFor(OauthController) | ||
class OauthControllerSpec extends Specification { | ||
|
||
def setup() { | ||
} | ||
|
||
def cleanup() { | ||
} | ||
|
||
void "test something"() { | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
test/unit/com/odobo/grails/plugin/springsecurity/rest/OauthServiceSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.odobo.grails.plugin.springsecurity.rest | ||
|
||
import grails.test.mixin.TestFor | ||
import spock.lang.Specification | ||
|
||
/** | ||
* See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions | ||
*/ | ||
@TestFor(OauthService) | ||
class OauthServiceSpec extends Specification { | ||
|
||
def setup() { | ||
} | ||
|
||
def cleanup() { | ||
} | ||
|
||
void "test something"() { | ||
} | ||
} |