Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publishing RestTokenCreationEvent from RestOauthController. Fixes #312 #401

Merged
merged 1 commit into from Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,6 +17,7 @@
package grails.plugin.springsecurity.rest

import grails.plugin.springsecurity.annotation.Secured
import grails.plugin.springsecurity.rest.authentication.RestAuthenticationEventPublisher
import grails.plugin.springsecurity.rest.error.CallbackErrorHandler
import grails.plugin.springsecurity.rest.token.AccessToken
import grails.plugin.springsecurity.rest.token.rendering.AccessTokenJsonRenderer
Expand Down Expand Up @@ -48,7 +49,7 @@ class RestOauthController {
TokenStorageService tokenStorageService
def tokenGenerator
AccessTokenJsonRenderer accessTokenJsonRenderer

RestAuthenticationEventPublisher authenticationEventPublisher
/**
* Starts the OAuth authentication flow, redirecting to the provider's Login URL. An optional callback parameter
* allows the frontend application to define the frontend callback URL on demand.
Expand Down Expand Up @@ -135,6 +136,8 @@ class RestOauthController {
AccessToken accessToken = tokenGenerator.generateAccessToken(principal, false)
accessToken.refreshToken = refreshToken

authenticationEventPublisher.publishTokenCreation(accessToken)

response.addHeader 'Cache-Control', 'no-store'
response.addHeader 'Pragma', 'no-cache'
render contentType: 'application/json', encoding: 'UTF-8', text: accessTokenJsonRenderer.generateJson(accessToken)
Expand Down
Expand Up @@ -16,9 +16,15 @@
*/
package grails.plugin.springsecurity.rest

import grails.plugin.springsecurity.rest.authentication.RestAuthenticationEventPublisher
import grails.plugin.springsecurity.rest.error.DefaultCallbackErrorHandler
import grails.plugin.springsecurity.rest.token.AccessToken
import grails.plugin.springsecurity.rest.token.generation.TokenGenerator
import grails.plugin.springsecurity.rest.token.storage.TokenStorageService
import grails.testing.web.controllers.ControllerUnitTest
import groovy.transform.InheritConstructors
import org.springframework.security.core.userdetails.User
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UsernameNotFoundException
import spock.lang.Issue
import spock.lang.Specification
Expand Down Expand Up @@ -117,6 +123,27 @@ class RestOauthControllerSpec extends Specification implements ControllerUnitTes
String expectedUrl = getExpectedUrl(caughtException, INTERNAL_SERVER_ERROR.value())
response.redirectedUrl == expectedUrl
}

void "it publishes RestTokenCreationEvent's"() {
given:
TokenStorageService stubbedTokenStorageService = Stub(TokenStorageService)
stubbedTokenStorageService.loadUserByToken(_) >> new User('foo', '', [])
controller.tokenStorageService = stubbedTokenStorageService

def stubbedTokenGenerator = [ generateAccessToken: { u,b -> new AccessToken('accessToken') }]
controller.tokenGenerator = stubbedTokenGenerator

controller.authenticationEventPublisher = Mock(RestAuthenticationEventPublisher)

when:
params.grant_type = 'refresh_token'
params.refresh_token = 'refresh_token'
request.method = 'POST'
controller.accessToken()

then:
1 * controller.authenticationEventPublisher.publishTokenCreation(_)
}
}

@InheritConstructors
Expand Down