From 0f2fea091b053a30c418532439c98ee13794767a Mon Sep 17 00:00:00 2001 From: DOHA Date: Wed, 28 Nov 2018 13:59:16 +0200 Subject: [PATCH] add more custom oauth examples --- .../CustomAuthorizationRequestResolver.java | 7 ++++++ .../LinkedinTokenResponseConverter.java | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 spring-5-security-oauth/src/main/java/com/baeldung/oauth2/LinkedinTokenResponseConverter.java diff --git a/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/CustomAuthorizationRequestResolver.java b/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/CustomAuthorizationRequestResolver.java index 025064423d..b3fcd15a9a 100644 --- a/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/CustomAuthorizationRequestResolver.java +++ b/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/CustomAuthorizationRequestResolver.java @@ -47,4 +47,11 @@ public class CustomAuthorizationRequestResolver implements OAuth2AuthorizationRe private OAuth2AuthorizationRequest customizeAuthorizationRequest1(OAuth2AuthorizationRequest req) { return OAuth2AuthorizationRequest.from(req).state("xyz").build(); } + + private OAuth2AuthorizationRequest customizeOktaReq(OAuth2AuthorizationRequest req) { + Map extraParams = new HashMap(); + extraParams.putAll(req.getAdditionalParameters()); + extraParams.put("idp", "https://idprovider.com"); + return OAuth2AuthorizationRequest.from(req).additionalParameters(extraParams).build(); + } } diff --git a/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/LinkedinTokenResponseConverter.java b/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/LinkedinTokenResponseConverter.java new file mode 100644 index 0000000000..f638b6101a --- /dev/null +++ b/spring-5-security-oauth/src/main/java/com/baeldung/oauth2/LinkedinTokenResponseConverter.java @@ -0,0 +1,24 @@ +package com.baeldung.oauth2; + +import java.util.Map; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.security.oauth2.core.OAuth2AccessToken; +import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; + +public class LinkedinTokenResponseConverter implements Converter, OAuth2AccessTokenResponse> { + + @Override + public OAuth2AccessTokenResponse convert(Map tokenResponseParameters) { + String accessToken = tokenResponseParameters.get(OAuth2ParameterNames.ACCESS_TOKEN); + long expiresIn = Long.valueOf(tokenResponseParameters.get(OAuth2ParameterNames.EXPIRES_IN)); + + OAuth2AccessToken.TokenType accessTokenType = OAuth2AccessToken.TokenType.BEARER; + + return OAuth2AccessTokenResponse.withToken(accessToken) + .tokenType(accessTokenType) + .expiresIn(expiresIn) + .build(); + } +}