-
-
Notifications
You must be signed in to change notification settings - Fork 654
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #532 from juliensadaoui/feat/511-oauth0-support
Add support for Auth0
- Loading branch information
Showing
16 changed files
with
273 additions
and
94 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
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 was deleted.
Oops, something went wrong.
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
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,3 @@ | ||
export class Logout { | ||
constructor(public logoutUrl: string) {} | ||
} |
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
93 changes: 93 additions & 0 deletions
93
src/test/java/tech/jhipster/registry/utils/OAuth2TestUtil.java
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,93 @@ | ||
package tech.jhipster.registry.utils; | ||
|
||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService; | ||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistration; | ||
import org.springframework.security.oauth2.core.OAuth2AccessToken; | ||
import org.springframework.security.oauth2.core.OAuth2AccessToken.TokenType; | ||
import org.springframework.security.oauth2.core.oidc.OidcIdToken; | ||
import org.springframework.security.oauth2.core.oidc.OidcUserInfo; | ||
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser; | ||
import tech.jhipster.registry.security.AuthoritiesConstants; | ||
import tech.jhipster.registry.security.SecurityUtils; | ||
|
||
public class OAuth2TestUtil { | ||
|
||
public static final String TEST_USER_LOGIN = "test"; | ||
|
||
public static final String ID_TOKEN = | ||
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" + | ||
".eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsIm" + | ||
"p0aSI6ImQzNWRmMTRkLTA5ZjYtNDhmZi04YTkzLTdjNmYwMzM5MzE1OSIsImlhdCI6MTU0M" + | ||
"Tk3MTU4MywiZXhwIjoxNTQxOTc1MTgzfQ.QaQOarmV8xEUYV7yvWzX3cUE_4W1luMcWCwpr" + | ||
"oqqUrg"; | ||
|
||
public static OAuth2AuthenticationToken testAuthenticationToken() { | ||
Map<String, Object> claims = new HashMap<>(); | ||
claims.put("sub", TEST_USER_LOGIN); | ||
claims.put("preferred_username", TEST_USER_LOGIN); | ||
claims.put("email", "[email protected]"); | ||
claims.put("roles", Collections.singletonList(AuthoritiesConstants.ADMIN)); | ||
|
||
return authenticationToken(claims); | ||
} | ||
|
||
public static OAuth2AuthenticationToken authenticationToken(Map<String, Object> claims) { | ||
Instant issuedAt = Instant.now(); | ||
Instant expiresAt = Instant.now().plus(1, ChronoUnit.DAYS); | ||
if (!claims.containsKey("sub")) { | ||
claims.put("sub", "jane"); | ||
} | ||
if (!claims.containsKey("preferred_username")) { | ||
claims.put("preferred_username", "jane"); | ||
} | ||
if (!claims.containsKey("email")) { | ||
claims.put("email", "[email protected]"); | ||
} | ||
if (claims.containsKey("auth_time")) { | ||
issuedAt = (Instant) claims.get("auth_time"); | ||
} else { | ||
claims.put("auth_time", issuedAt); | ||
} | ||
if (claims.containsKey("exp")) { | ||
expiresAt = (Instant) claims.get("exp"); | ||
} else { | ||
claims.put("exp", expiresAt); | ||
} | ||
Collection<GrantedAuthority> authorities = SecurityUtils.extractAuthorityFromClaims(claims); | ||
OidcIdToken token = new OidcIdToken(ID_TOKEN, issuedAt, expiresAt, claims); | ||
OidcUserInfo userInfo = new OidcUserInfo(claims); | ||
DefaultOidcUser user = new DefaultOidcUser(authorities, token, userInfo, "preferred_username"); | ||
return new OAuth2AuthenticationToken(user, user.getAuthorities(), "oidc"); | ||
} | ||
|
||
public static OAuth2AuthenticationToken registerAuthenticationToken( | ||
OAuth2AuthorizedClientService authorizedClientService, | ||
ClientRegistration clientRegistration, | ||
OAuth2AuthenticationToken authentication | ||
) { | ||
Map<String, Object> userDetails = authentication.getPrincipal().getAttributes(); | ||
|
||
OAuth2AccessToken token = new OAuth2AccessToken( | ||
TokenType.BEARER, | ||
"Token", | ||
(Instant) userDetails.get("auth_time"), | ||
(Instant) userDetails.get("exp") | ||
); | ||
|
||
authorizedClientService.saveAuthorizedClient( | ||
new OAuth2AuthorizedClient(clientRegistration, authentication.getName(), token), | ||
authentication | ||
); | ||
|
||
return authentication; | ||
} | ||
} |
Oops, something went wrong.