Skip to content

Instantly share code, notes, and snippets.

@CalamarBicefalo
Created April 11, 2017 14:57
Show Gist options
  • Save CalamarBicefalo/b4cd484c037b0ccfce96ef20fae81f03 to your computer and use it in GitHub Desktop.
Save CalamarBicefalo/b4cd484c037b0ccfce96ef20fae81f03 to your computer and use it in GitHub Desktop.
OAuth2 MockMvc helper to retrieve valid oauth2 tokens
import org.springframework.security.authentication.TestingAuthenticationToken
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.User
import org.springframework.security.oauth2.common.OAuth2AccessToken
import org.springframework.security.oauth2.provider.ClientDetailsService
import org.springframework.security.oauth2.provider.OAuth2Authentication
import org.springframework.security.oauth2.provider.OAuth2Request
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter
import org.springframework.stereotype.Component
import org.springframework.test.web.servlet.request.RequestPostProcessor
import java.io.Serializable
import java.util.Collections
@Component
class OAuth2Helper(
val clientDetailsService: ClientDetailsService,
val tokenservice: AuthorizationServerTokenServices,
val tokenenhancer: JwtAccessTokenConverter) {
// For use with MockMvc
fun bearerTokenForClient(clientid: String): RequestPostProcessor {
return RequestPostProcessor { mockRequest ->
val token = createAccessToken(clientid)
mockRequest.addHeader("Authorization", "Bearer " + token.value)
mockRequest
}
}
// For use with MockMvc
fun bearerTokenForUser(username: String, authorities: Array<String>, clientid: String = "client_id"): RequestPostProcessor {
return RequestPostProcessor { mockRequest ->
val token = createAccessToken(clientid, username, authorities)
mockRequest.addHeader("Authorization", "Bearer " + token.value)
mockRequest
}
}
// For use with MockMvc
fun bearerToken(token: String): RequestPostProcessor {
return RequestPostProcessor { mockRequest ->
mockRequest.addHeader("Authorization", "Bearer " + token)
mockRequest
}
}
fun createAccessToken(clientId: String, username: String? = null, userAuthorities: Array<String> = emptyArray()): OAuth2AccessToken {
// Look up authorities, resourceIds and scopes based on clientId
val client = clientDetailsService.loadClientByClientId(clientId)
val clientAuthorities = client.authorities
val resourceIds = client.resourceIds
val scopes = client.scope
// Default values for other parameters
val requestParameters: Map<String, String> = Collections.emptyMap()
val approved = true
val redirectUrl: String? = null
val responseTypes = Collections.emptySet<String>()
val extensionProperties = Collections.emptyMap<String, Serializable>()
// Create request
val oAuth2Request = OAuth2Request(requestParameters, clientId, clientAuthorities, approved, scopes,
resourceIds, redirectUrl, responseTypes, extensionProperties)
// Create OAuth2AccessToken
val auth = if (username != null) {
val grantedAuthorities = userAuthorities.map(::SimpleGrantedAuthority)
val userPrincipal = User(username, "", true, true, true, true, emptyList())
val authenticationToken = TestingAuthenticationToken(userPrincipal, null, grantedAuthorities)
OAuth2Authentication(oAuth2Request, authenticationToken)
} else {
OAuth2Authentication(oAuth2Request, null)
}
val token = tokenservice.createAccessToken(auth)
return tokenenhancer.enhance(token, auth)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment