Skip to content

Instantly share code, notes, and snippets.

@CalamarBicefalo
Created April 11, 2017 14:49
Show Gist options
  • Save CalamarBicefalo/ef69db4ee297b2eba925150724150027 to your computer and use it in GitHub Desktop.
Save CalamarBicefalo/ef69db4ee297b2eba925150724150027 to your computer and use it in GitHub Desktop.
Kotlin extension methods to configure resttemplate to support various oauth2 grant types
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpRequest
import org.springframework.http.MediaType
import org.springframework.http.client.ClientHttpRequestExecution
import org.springframework.http.client.ClientHttpRequestInterceptor
import org.springframework.http.client.ClientHttpResponse
import org.springframework.util.Assert
import org.springframework.util.Base64Utils
import org.springframework.util.LinkedMultiValueMap
import org.springframework.util.MultiValueMap
import java.nio.charset.StandardCharsets
class BearerAuthorizationInterceptor(private val token: String) : ClientHttpRequestInterceptor {
init {
Assert.hasLength(token, "Token must not be empty")
}
override fun intercept(request: HttpRequest, body: ByteArray, execution: ClientHttpRequestExecution): ClientHttpResponse {
request.headers.add("Authorization", "Bearer " + token)
return execution.execute(request, body)
}
}
fun RestTemplateBuilder.oauth2ClientAuthorization(clientId: String, clientSecret: String): RestTemplateBuilder {
val restTemplate = this.build()
val headers = createClientCredentialsHeader(clientId, clientSecret)
val map = LinkedMultiValueMap<String, String>()
map.add("grant_type", "client_credentials")
val request = HttpEntity<MultiValueMap<String, String>>(map, headers)
val token = restTemplate.postForObject("/oauth/token", request, Map::class.java)["access_token"].toString()
return this.interceptors(BearerAuthorizationInterceptor(token))
}
private fun createClientCredentialsHeader(clientId: String, clientSecret: String): HttpHeaders {
val headers = HttpHeaders()
headers.contentType = MediaType.APPLICATION_FORM_URLENCODED
val encodedAuth = Base64Utils.encodeToString("$clientId:$clientSecret".toByteArray(StandardCharsets.UTF_8))
headers["Authorization"] = "Basic $encodedAuth"
return headers
}
fun RestTemplateBuilder.oauth2PasswordAuthorization(clientId: String, clientSecret: String, username: String, password: String): RestTemplateBuilder {
val restTemplate = this.build()
val headers = createClientCredentialsHeader(clientId, clientSecret)
val map = LinkedMultiValueMap<String, String>()
map.add("grant_type", "password")
map.add("username", username)
map.add("password", password)
val request = HttpEntity<MultiValueMap<String, String>>(map, headers)
val token = restTemplate.postForObject("/oauth/token", request, Map::class.java)["access_token"].toString()
return this.interceptors(BearerAuthorizationInterceptor(token))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment