roles : accessCodeEntry.getResourceRolesRequested().values()) {
- for (RoleModel role : roles) {
- addComposites(token, role);
- }
- }
- }
- accessCodeEntry.setToken(token);
- }
-
public String encodeScope(AccessScope scope) {
String token = null;
try {
@@ -224,8 +238,8 @@ public class TokenManager {
token.issuedNow();
token.subject(user.getId());
token.audience(realm.getName());
- if (realm.getTokenLifespan() > 0) {
- token.expiration((System.currentTimeMillis() / 1000) + realm.getTokenLifespan());
+ if (realm.getAccessTokenLifespan() > 0) {
+ token.expiration((System.currentTimeMillis() / 1000) + realm.getAccessTokenLifespan());
}
for (RoleModel role : realm.getRoleMappings(user)) {
addComposites(token, role);
diff --git a/services/src/main/java/org/keycloak/services/resources/PublicRealmResource.java b/services/src/main/java/org/keycloak/services/resources/PublicRealmResource.java
index 1b699aae9c..944f1a99bf 100755
--- a/services/src/main/java/org/keycloak/services/resources/PublicRealmResource.java
+++ b/services/src/main/java/org/keycloak/services/resources/PublicRealmResource.java
@@ -53,13 +53,11 @@ public class PublicRealmResource {
String authUri = TokenService.loginPageUrl(uriInfo).build(realm.getName()).toString();
String codeUri = TokenService.accessCodeToTokenUrl(uriInfo).build(realm.getName()).toString();
String grantUrl = TokenService.grantAccessTokenUrl(uriInfo).build(realm.getName()).toString();
- String idGrantUrl = TokenService.grantIdentityTokenUrl(uriInfo).build(realm.getName()).toString();
html.append("Realm: ").append(realm.getName()).append("
");
html.append("auth: ").append(authUri).append("
");
html.append("code: ").append(codeUri).append("
");
html.append("grant: ").append(grantUrl).append("
");
- html.append("identity grant: ").append(idGrantUrl).append("
");
html.append("public key: ").append(realm.getPublicKeyPem()).append("
");
html.append("");
@@ -77,8 +75,6 @@ public class PublicRealmResource {
rep.setAuthorizationUrl(TokenService.loginPageUrl(uriInfo).build(realm.getName()).toString());
rep.setCodeUrl(TokenService.accessCodeToTokenUrl(uriInfo).build(realm.getName()).toString());
rep.setGrantUrl(TokenService.grantAccessTokenUrl(uriInfo).build(realm.getName()).toString());
- String idGrantUrl = TokenService.grantIdentityTokenUrl(uriInfo).build(realm.getName()).toString();
- rep.setIdentityGrantUrl(idGrantUrl);
return rep;
}
diff --git a/services/src/main/java/org/keycloak/services/resources/TokenService.java b/services/src/main/java/org/keycloak/services/resources/TokenService.java
index cf86534886..f38c985174 100755
--- a/services/src/main/java/org/keycloak/services/resources/TokenService.java
+++ b/services/src/main/java/org/keycloak/services/resources/TokenService.java
@@ -29,6 +29,7 @@ import org.keycloak.services.resources.flows.OAuthFlows;
import org.keycloak.services.validation.Validation;
import org.keycloak.util.BasicAuthHelper;
+import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
@@ -108,11 +109,6 @@ public class TokenService {
}
- public static UriBuilder grantIdentityTokenUrl(UriInfo uriInfo) {
- return tokenServiceBaseUrl(uriInfo).path(TokenService.class, "grantIdentityToken");
-
- }
-
public static UriBuilder loginPageUrl(UriInfo uriInfo) {
return tokenServiceBaseUrl(uriInfo).path(TokenService.class, "loginPage");
}
@@ -129,45 +125,19 @@ public class TokenService {
return tokenServiceBaseUrl(uriInfo).path(TokenService.class, "processOAuth");
}
- @Path("grants/identity-token")
- @POST
- @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
- @Produces(MediaType.APPLICATION_JSON)
- public Response grantIdentityToken(final MultivaluedMap form) {
- if (!checkSsl()) {
- throw new NotAcceptableException("HTTPS required");
- }
-
- String username = form.getFirst(AuthenticationManager.FORM_USERNAME);
- if (username == null) {
- throw new NotAuthorizedException("No user");
- }
- if (!realm.isEnabled()) {
- throw new NotAuthorizedException("Disabled realm");
- }
- UserModel user = realm.getUser(username);
-
- AuthenticationStatus status = authManager.authenticateForm(realm, user, form);
- if (status != AuthenticationStatus.SUCCESS) {
- throw new NotAuthorizedException(status);
- }
-
- tokenManager = new TokenManager();
- AccessToken token = authManager.createIdentityToken(realm, user);
- String encoded = tokenManager.encodeToken(realm, token);
- AccessTokenResponse res = accessTokenResponse(token, encoded);
- return Response.ok(res, MediaType.APPLICATION_JSON_TYPE).build();
- }
-
@Path("grants/access")
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
- public Response grantAccessToken(final MultivaluedMap form) {
+ public Response grantAccessToken(final @HeaderParam(HttpHeaders.AUTHORIZATION) String authorizationHeader,
+ final MultivaluedMap form) {
if (!checkSsl()) {
throw new NotAcceptableException("HTTPS required");
}
+ UserModel client = authorizeClient(authorizationHeader);
+
+
String username = form.getFirst(AuthenticationManager.FORM_USERNAME);
if (username == null) {
throw new NotAuthorizedException("No user");
@@ -185,7 +155,8 @@ public class TokenService {
if (authManager.authenticateForm(realm, user, form) != AuthenticationStatus.SUCCESS) {
throw new NotAuthorizedException("Auth failed");
}
- AccessToken token = tokenManager.createAccessToken(realm, user);
+ String scope = form.getFirst("scope");
+ AccessToken token = tokenManager.createClientAccessToken(scope, realm, client, user);
String encoded = tokenManager.encodeToken(realm, token);
AccessTokenResponse res = accessTokenResponse(token, encoded);
return Response.ok(res, MediaType.APPLICATION_JSON_TYPE).build();
@@ -341,47 +312,14 @@ public class TokenService {
throw new NotAuthorizedException("Realm not enabled");
}
- if (authorizationHeader == null) {
- throw new NotAuthorizedException("No Authorization header to authenticate client", "Basic realm=\"" + realm.getName() + "\"");
- }
-
- String[] usernameSecret = BasicAuthHelper.parseHeader(authorizationHeader);
- if (usernameSecret == null) {
- throw new NotAuthorizedException("No Authorization header to authenticate client", "Basic realm=\"" + realm.getName() + "\"");
- }
-
- String client_id = usernameSecret[0];
- String clientSecret = usernameSecret[1];
- UserModel client = realm.getUser(client_id);
- if (client == null) {
- logger.debug("Could not find user");
- Map error = new HashMap();
- error.put("error", "invalid_client");
- error.put("error_description", "Could not find user");
- return Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build();
- }
-
- if (!client.isEnabled()) {
- logger.debug("user is not enabled");
- Map error = new HashMap();
- error.put("error", "invalid_client");
- error.put("error_description", "User is not enabled");
- return Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build();
- }
-
- if (!realm.validateSecret(client, clientSecret)) {
- Map error = new HashMap();
- error.put("error", "unauthorized_client");
- return Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build();
- }
+ UserModel client = authorizeClient(authorizationHeader);
String code = formData.getFirst("code");
if (code == null) {
- logger.debug("code not specified");
Map error = new HashMap();
error.put("error", "invalid_request");
error.put("error_description", "code not specified");
- return Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build();
+ throw new BadRequestException("Code not specified", Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build());
}
@@ -435,6 +373,41 @@ public class TokenService {
return Cors.add(request, Response.ok(res)).allowedOrigins(client).allowedMethods("POST").build();
}
+ protected UserModel authorizeClient(String authorizationHeader) {
+ if (authorizationHeader == null) {
+ throw new NotAuthorizedException("No Authorization header to authenticate client", "Basic realm=\"" + realm.getName() + "\"");
+ }
+
+ String[] usernameSecret = BasicAuthHelper.parseHeader(authorizationHeader);
+ if (usernameSecret == null) {
+ throw new NotAuthorizedException("No Authorization header to authenticate client", "Basic realm=\"" + realm.getName() + "\"");
+ }
+
+ String client_id = usernameSecret[0];
+ String clientSecret = usernameSecret[1];
+ UserModel client = realm.getUser(client_id);
+ if (client == null) {
+ Map error = new HashMap();
+ error.put("error", "invalid_client");
+ error.put("error_description", "Could not find client");
+ throw new BadRequestException("Could not find client", Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build());
+ }
+
+ if (!client.isEnabled()) {
+ Map error = new HashMap();
+ error.put("error", "invalid_client");
+ error.put("error_description", "Client is not enabled");
+ throw new BadRequestException("Client is not enabled", Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build());
+ }
+
+ if (!realm.validateSecret(client, clientSecret)) {
+ Map error = new HashMap();
+ error.put("error", "unauthorized_client");
+ throw new BadRequestException("Unauthorized Client", Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build());
+ }
+ return client;
+ }
+
protected AccessTokenResponse accessTokenResponse(PrivateKey privateKey, AccessToken token) {
String encodedToken = new JWSBuilder().jsonContent(token).rsa256(privateKey);
diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/composites/CompositeRoleTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/composites/CompositeRoleTest.java
index adc8bf54c9..720ea52e3f 100755
--- a/testsuite/integration/src/test/java/org/keycloak/testsuite/composites/CompositeRoleTest.java
+++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/composites/CompositeRoleTest.java
@@ -57,7 +57,7 @@ public class CompositeRoleTest {
RealmModel realm = manager.createRealm("Test");
manager.generateRealmKeys(realm);
realmPublicKey = realm.getPublicKey();
- realm.setTokenLifespan(10000);
+ realm.setAccessTokenLifespan(10000);
realm.setAccessCodeLifespanUserAction(1000);
realm.setAccessCodeLifespan(1000);
realm.setSslNotRequired(true);
diff --git a/testsuite/integration/src/test/resources/testcomposite.json b/testsuite/integration/src/test/resources/testcomposite.json
index 19b662e9f5..cfdd2c73c5 100755
--- a/testsuite/integration/src/test/resources/testcomposite.json
+++ b/testsuite/integration/src/test/resources/testcomposite.json
@@ -2,7 +2,7 @@
"id": "Test",
"realm": "Test",
"enabled": true,
- "tokenLifespan": 600,
+ "accessTokenLifespan": 600,
"accessCodeLifespan": 600,
"accessCodeLifespanUserAction": 600,
"sslNotRequired": true,
diff --git a/testsuite/integration/src/test/resources/testrealm.json b/testsuite/integration/src/test/resources/testrealm.json
index e040c2cd4b..72db7ca928 100755
--- a/testsuite/integration/src/test/resources/testrealm.json
+++ b/testsuite/integration/src/test/resources/testrealm.json
@@ -2,7 +2,7 @@
"id": "test",
"realm": "test",
"enabled": true,
- "tokenLifespan": 600,
+ "accessTokenLifespan": 600,
"accessCodeLifespan": 600,
"accessCodeLifespanUserAction": 600,
"sslNotRequired": true,