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

initial implementation of token exchange (only access tokens) #34

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
plugins {
id 'org.springframework.boot' version '2.5.4'
id 'org.springframework.boot' version '2.6.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'com.adarshr.test-logger' version '2.1.1'
id 'org.owasp.dependencycheck' version '6.1.2'
id 'org.owasp.dependencycheck' version '6.5.1'
}

group = 'com.example'
Expand All @@ -26,8 +26,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springdoc:springdoc-openapi-ui:1.5.10'
implementation 'com.nimbusds:nimbus-jose-jwt:9.13'
implementation 'org.springdoc:springdoc-openapi-ui:1.6.2'
implementation 'com.nimbusds:nimbus-jose-jwt:9.15.2'
implementation 'org.apache.commons:commons-lang3'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2'
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
10 changes: 10 additions & 0 deletions src/main/java/com/example/authorizationserver/DataInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ private void createClients() {
false,
AccessTokenFormat.OPAQUE,
Set.of(GrantType.AUTHORIZATION_CODE),
Collections.singleton(
"http://localhost:8080/demo-client/login/oauth2/code/demo"),
Collections.singleton("*")),
new RegisteredClient(
UUID.randomUUID(),
"token-exchange",
passwordEncoder.encode("demo"),
true,
AccessTokenFormat.JWT,
Set.of(GrantType.TOKEN_EXCHANGE),
Collections.singleton(
"http://localhost:8080/demo-client/login/oauth2/code/demo"),
Collections.singleton("*")))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.authorizationserver.oauth.common;

public enum TokenType {

// Indicates that the token is an OAuth 2.0 access token
ACCESS_TOKEN("urn:ietf:params:oauth:token-type:access_token"),

// Indicates that the token is an OAuth 2.0 refresh token.
REFRESH_TOKEN("urn:ietf:params:oauth:token-type:refresh_token"),

// Indicates that the token is an ID Token as defined in OpenID.Core.
ID_TOKEN("urn:ietf:params:oauth:token-type:id_token"),

// Indicates that the token is a base64url-encoded SAML 1.1 assertion.
SAML11_TOKEN("urn:ietf:params:oauth:token-type:saml1"),

// Indicates that the token is a base64url-encoded SAML 2.0 assertion.
SAML2_TOKEN("urn:ietf:params:oauth:token-type:saml2"),

// Indicated that the token is a JSON web token.
JWT_TOKEN("urn:ietf:params:oauth:token-type:jwt");

private final String identifier;

TokenType(String identifier) {
this.identifier = identifier;
}

public String getIdentifier() {
return identifier;
}

public static TokenType getTokenTypeForIdentifier(String identifier) {
if (ACCESS_TOKEN.getIdentifier().equals(identifier)) {
return ACCESS_TOKEN;
} else if (REFRESH_TOKEN.getIdentifier().equals(identifier)) {
return REFRESH_TOKEN;
} else if (ID_TOKEN.getIdentifier().equals(identifier)) {
return ID_TOKEN;
} else if (SAML11_TOKEN.getIdentifier().equals(identifier)) {
return SAML11_TOKEN;
} else if (SAML2_TOKEN.getIdentifier().equals(identifier)) {
return SAML2_TOKEN;
} else if (JWT_TOKEN.getIdentifier().equals(identifier)) {
return JWT_TOKEN;
} else {
throw new IllegalArgumentException("Invalid token type " + identifier);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ public class TokenEndpoint {
private final PasswordTokenEndpointService passwordTokenEndpointService;
private final RefreshTokenEndpointService refreshTokenEndpointService;
private final AuthorizationCodeTokenEndpointService authorizationCodeTokenEndpointService;
private final TokenExchangeEndpointService tokenExchangeEndpointService;

public TokenEndpoint(
ClientCredentialsTokenEndpointService clientCredentialsTokenEndpointService,
PasswordTokenEndpointService passwordTokenEndpointService,
RefreshTokenEndpointService refreshTokenEndpointService,
AuthorizationCodeTokenEndpointService authorizationCodeTokenEndpointService) {
AuthorizationCodeTokenEndpointService authorizationCodeTokenEndpointService, TokenExchangeEndpointService tokenExchangeEndpointService) {
this.clientCredentialsTokenEndpointService = clientCredentialsTokenEndpointService;
this.passwordTokenEndpointService = passwordTokenEndpointService;
this.refreshTokenEndpointService = refreshTokenEndpointService;
this.authorizationCodeTokenEndpointService = authorizationCodeTokenEndpointService;
this.tokenExchangeEndpointService = tokenExchangeEndpointService;
}

@PostMapping
Expand All @@ -63,8 +65,7 @@ public ResponseEntity<TokenResponse> getToken(
return refreshTokenEndpointService.getTokenResponseForRefreshToken(
authorizationHeader, tokenRequest);
} else if (tokenRequest.getGrant_type().equalsIgnoreCase(GrantType.TOKEN_EXCHANGE.getGrant())) {
LOG.warn("Requested grant type for 'Token Exchange' is not yet supported");
return ResponseEntity.badRequest().body(new TokenResponse("unsupported_grant_type"));
return tokenExchangeEndpointService.getTokenResponseForTokenExchange(authorizationHeader, tokenRequest);
} else {
LOG.warn("Requested grant type [{}] is unsupported", tokenRequest.getGrant_type());
return ResponseEntity.badRequest().body(new TokenResponse("unsupported_grant_type"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ static ResponseEntity<TokenResponse> reportInvalidClientError() {
static ResponseEntity<TokenResponse> reportInvalidGrantError() {
return ResponseEntity.badRequest().body(new TokenResponse("invalid_grant"));
}

static ResponseEntity<TokenResponse> reportInvalidRequestError() {
return ResponseEntity.badRequest().body(new TokenResponse("invalid_request"));
}

static ResponseEntity<TokenResponse> reportInvalidTargetError() {
return ResponseEntity.badRequest().body(new TokenResponse("invalid_target"));
}

}
Loading