KEYCLOAK-38 Adding OAuth Grant to forms

This commit is contained in:
vrockai 2013-10-09 13:36:17 +02:00
parent 0f18eb5341
commit 5ec74be39e
12 changed files with 539 additions and 18 deletions

View file

@ -0,0 +1,88 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.keycloak.forms;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.MultivaluedMap;
import org.keycloak.models.RoleModel;
import org.keycloak.models.UserModel;
/**
* @author <a href="mailto:vrockai@redhat.com">Viliam Rockai</a>
*/
public class OAuthGrantBean {
private MultivaluedMap<String, RoleModel> resourceRolesRequested;
private List<RoleModel> realmRolesRequested;
private UserModel client;
private String oAuthCode;
private String action;
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public List<String> getResourceNames(){
return new ArrayList<String>(resourceRolesRequested.keySet());
}
public MultivaluedMap<String, RoleModel> getResourceRolesRequested() {
return resourceRolesRequested;
}
public void setResourceRolesRequested(MultivaluedMap<String, RoleModel> resourceRolesRequested) {
this.resourceRolesRequested = resourceRolesRequested;
}
public List<RoleModel> getRealmRolesRequested() {
return realmRolesRequested;
}
public void setRealmRolesRequested(List<RoleModel> realmRolesRequested) {
this.realmRolesRequested = realmRolesRequested;
}
public UserModel getClient() {
return client;
}
public void setClient(UserModel client) {
this.client = client;
}
// lowercase "o" needed for FM template to access the property
public String getoAuthCode() {
return oAuthCode;
}
public void setoAuthCode(String oAuthCode) {
this.oAuthCode = oAuthCode;
}
}

View file

@ -34,6 +34,7 @@ import freemarker.template.TemplateException;
import org.jboss.resteasy.logging.Logger;
import org.keycloak.forms.ErrorBean;
import org.keycloak.forms.LoginBean;
import org.keycloak.forms.OAuthGrantBean;
import org.keycloak.forms.RealmBean;
import org.keycloak.forms.RegisterBean;
import org.keycloak.forms.SocialBean;
@ -42,7 +43,6 @@ import org.keycloak.forms.TotpBean;
import org.keycloak.forms.UrlBean;
import org.keycloak.forms.UserBean;
import org.keycloak.services.FormService;
import org.keycloak.services.resources.flows.FormFlows;
import org.keycloak.services.resources.flows.Pages;
/**
@ -71,6 +71,7 @@ public class FormServiceImpl implements FormService {
commandMap.put(Pages.LOGIN_TOTP, new CommandLoginTotp());
commandMap.put(Pages.LOGIN_VERIFY_EMAIL, new CommandLoginTotp());
commandMap.put(Pages.ERROR, new CommandError());
commandMap.put(Pages.OAUTH_GRANT, new CommandOAuthGrant());
}
public String getId(){
@ -259,6 +260,20 @@ public class FormServiceImpl implements FormService {
}
}
private class CommandOAuthGrant implements Command {
public void exec(Map<String, Object> attributes, FormServiceDataBean dataBean) {
OAuthGrantBean oauth = new OAuthGrantBean();
oauth.setAction(dataBean.getOAuthAction());
oauth.setResourceRolesRequested(dataBean.getOAuthResourceRolesRequested());
oauth.setClient(dataBean.getOAuthClient());
oauth.setoAuthCode(dataBean.getOAuthCode());
oauth.setRealmRolesRequested(dataBean.getOAuthRealmRolesRequested());
attributes.put("oauth", oauth);
}
}
private interface Command {
public void exec(Map<String, Object> attributes, FormServiceDataBean dataBean);
}

View file

@ -102,6 +102,16 @@ a.button.disabled {
font-weight: normal;
letter-spacing: 0.06363636363636em;
}
input[type="button"].btn-secondary,
button.btn-secondary,
input[type="submit"].btn-secondary,
a.button.btn-secondary {
background-color: #EEEEEE;
border-color: #BBBBBB;
color: #4D5258;
}
input[type="button"].disabled:hover,
input[type="submit"].disabled:hover,
button.disabled:hover,

View file

@ -11,20 +11,32 @@
<#elseif section = "form">
<div class="content-area">
<p class="instruction">This application requests access to:</p>
<p class="instruction"><strong>${oauth.client.loginName}</strong> requests access to:</p>
<ul>
<li>
<span>View basic information about your account</span>
</li>
<li>
<span>View your email address</span>
</li>
<#list oauth.realmRolesRequested as role>
<li>
<span>${role.description}</span>
</li>
</#list>
</ul>
<#list oauth.resourceRolesRequested?keys as resourceRole>
<p class="instruction"><strong>${resourceRole}</strong> requests access to:</p>
<ul>
<#list oauth.resourceRolesRequested[resourceRole] as role>
<li>
<span>${role.description}</span>
</li>
</#list>
</ul>
</#list>
<p class="terms">Keycloak Central Login and Google will use this information in accordance with their respective terms of service and privacy policies.</p>
<div class="form-actions">
<button class="primary" type="submit">Accept</button>
<button type="submit">Cancel</button>
</div>
<form class="form-actions" action="${oauth.action}" method="POST">
<input type="hidden" name="code" value="${oauth.oAuthCode}">
<input type="submit" class="btn-primary primary" name="accept" value="Accept">
<input type="submit" class="btn-secondary" name="cancel" value="Cancel">
</form>
</div>
<#elseif section = "info" >

View file

@ -22,10 +22,12 @@
package org.keycloak.services;
import java.net.URI;
import java.util.List;
import javax.ws.rs.core.MultivaluedMap;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleModel;
import org.keycloak.models.UserModel;
import org.keycloak.services.resources.flows.FormFlows;
@ -133,5 +135,53 @@ public interface FormService {
public void setErrorType(FormFlows.ErrorType errorType) {
this.errorType = errorType;
}
/* OAuth Part */
private MultivaluedMap<String, RoleModel> oAuthResourceRolesRequested;
private List<RoleModel> oAuthRealmRolesRequested;
private UserModel oAuthClient;
private String oAuthCode;
private String oAuthAction;
public String getOAuthAction() {
return oAuthAction;
}
public void setOAuthAction(String action) {
this.oAuthAction = action;
}
public MultivaluedMap<String, RoleModel> getOAuthResourceRolesRequested() {
return oAuthResourceRolesRequested;
}
public void setOAuthResourceRolesRequested(MultivaluedMap<String, RoleModel> resourceRolesRequested) {
this.oAuthResourceRolesRequested = resourceRolesRequested;
}
public List<RoleModel> getOAuthRealmRolesRequested() {
return oAuthRealmRolesRequested;
}
public void setOAuthRealmRolesRequested(List<RoleModel> realmRolesRequested) {
this.oAuthRealmRolesRequested = realmRolesRequested;
}
public UserModel getOAuthClient() {
return oAuthClient;
}
public void setOAuthClient(UserModel client) {
this.oAuthClient = client;
}
public String getOAuthCode() {
return oAuthCode;
}
public void setOAuthCode(String oAuthCode) {
this.oAuthCode = oAuthCode;
}
}
}

View file

@ -23,9 +23,11 @@ package org.keycloak.services.resources.flows;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.ResteasyUriInfo;
import org.keycloak.models.RoleModel;
import org.keycloak.services.FormService;
import org.keycloak.services.email.EmailSender;
import org.keycloak.services.managers.AccessCodeEntry;
@ -100,9 +102,7 @@ public class FormFlows {
return forwardToForm(Pages.ACCOUNT);
}
private Response forwardToForm(String template) {
FormService.FormServiceDataBean formDataBean = new FormService.FormServiceDataBean(realm, userModel, formData, error);
private Response forwardToForm(String template, FormService.FormServiceDataBean formDataBean) {
formDataBean.setErrorType(errorType == null ? ErrorType.ERROR : errorType);
// Getting URI needed by form processing service
@ -140,6 +140,13 @@ public class FormFlows {
return Response.status(200).entity("form provider not found").build();
}
private Response forwardToForm(String template) {
FormService.FormServiceDataBean formDataBean = new FormService.FormServiceDataBean(realm, userModel, formData, error);
return forwardToForm(template, formDataBean);
}
public Response forwardToLogin() {
return forwardToForm(Pages.LOGIN);
}
@ -172,6 +179,19 @@ public class FormFlows {
return forwardToForm(Pages.ERROR);
}
public Response forwardToOAuthGrant(){
FormService.FormServiceDataBean formDataBean = new FormService.FormServiceDataBean(realm, userModel, formData, error);
formDataBean.setOAuthRealmRolesRequested((List<RoleModel>) request.getAttribute("realmRolesRequested"));
formDataBean.setOAuthResourceRolesRequested((MultivaluedMap<String, RoleModel>) request.getAttribute("resourceRolesRequested"));
formDataBean.setOAuthClient((UserModel)request.getAttribute("client"));
formDataBean.setOAuthCode((String)request.getAttribute("code"));
formDataBean.setOAuthAction((String)request.getAttribute("action"));
return forwardToForm(Pages.OAUTH_GRANT, formDataBean);
}
public FormFlows setAccessCode(AccessCodeEntry accessCode) {
this.accessCode = accessCode;
return this;

View file

@ -121,8 +121,7 @@ public class OAuthFlows {
request.setAttribute("action", TokenService.processOAuthUrl(uriInfo).build(realm.getId()).toString());
request.setAttribute("code", accessCode.getCode());
request.forward(Pages.OAUTH_GRANT);
return null;
return Flows.forms(realm, request, uriInfo).setAccessCode(accessCode).forwardToOAuthGrant();
}
public Response forwardToSecurityFailure(String message) {

View file

@ -38,7 +38,7 @@ public class Pages {
public final static String LOGIN_VERIFY_EMAIL = "/forms/login-verify-email.ftl";
public final static String OAUTH_GRANT = "/saas/oauthGrantForm.jsp";
public final static String OAUTH_GRANT = "/forms/login-oauth-grant.ftl";
public final static String PASSWORD = "/forms/password.ftl";

View file

@ -0,0 +1,112 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.keycloak.testsuite;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.PublicKey;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.UriBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.keycloak.PemUtils;
import org.keycloak.RSATokenVerifier;
import org.keycloak.representations.SkeletonKeyToken;
import org.keycloak.servlet.ServletOAuthClient;
/**
* @author <a href="mailto:vrockai@redhat.com">Viliam Rockai</a>
*/
public class OAuthGrantServlet extends HttpServlet {
public static ServletOAuthClient client;
private static String baseUrl = Constants.AUTH_SERVER_ROOT + "/rest";
private static String realm = "test";
private static String realmKeyPem = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrVrCuTtArbgaZzL1hvh0xtL5mc7o0NqPVnYXkLvg" +
"cwiC3BjLGw1tGEGoJaXDuSaRllobm53JBhjx33UNv+5z/UMG4kytBWxheNVKnL6GgqlNabMaFfPLPCF8kAgKnsi79NMo+n6KnSY8YeUmec/" +
"p2vjO2NjsSAVcWEQMVhJ31LwIDAQAB";
public void init() {
client = new ServletOAuthClient();
client.setClientId("third-party");
client.setPassword("password");
client.setAuthUrl(UriBuilder.fromUri(baseUrl + "/realms/" + realm + "/tokens/login").build().toString());
client.setCodeUrl(UriBuilder.fromUri(baseUrl + "/realms/" + realm + "/tokens/access/codes").build().toString());
client.setClient(new ResteasyClientBuilder().build());
client.start();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
PrintWriter pw = resp.getWriter();
// Error "access_denied" happens after clicking on cancel when asked for granting permission
if (req.getParameterValues("error") != null){
pw.print("<html><head><title></title></head><body>Access rights not granted.</body></html>");
// Code is sent as a parameter in case that access was granted
} else if(req.getParameterValues("code") != null) {
String token = client.getBearerToken(req);
pw.print("<html><head><title></title></head><body>Access rights granted.<br/>Token:"+token+"<br/>");
// Check whether the token itself is relevant
try {
PublicKey realmKey = PemUtils.decodePublicKey(realmKeyPem);
SkeletonKeyToken skeletonToken = RSATokenVerifier.verifyToken(token, realmKey, realm);
// Writing app/role information on a page in format which is easy to assert in a test.
pw.print("Role:");
for(String role: skeletonToken.getRealmAccess().getRoles()){
pw.print(role);
}
pw.print(".<br/>");
for(Map.Entry<String, SkeletonKeyToken.Access> entry: skeletonToken.getResourceAccess().entrySet()){
pw.print("App:"+entry.getKey()+";");
for(String role: entry.getValue().getRoles()){
pw.print(role);
}
}
pw.print(".<br/>");
} catch (Exception e){
}
pw.print("</body></html>");
// If no code was sent or error happened, it's 1st visit to servlet and we need to ask for permissions
} else {
client.redirectRelative("", req, resp);
}
pw.flush();
}
}

View file

@ -0,0 +1,124 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.keycloak.testsuite.oauth;
import java.io.IOException;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.keycloak.testsuite.OAuthClient;
import org.keycloak.testsuite.OAuthGrantServlet;
import org.keycloak.testsuite.pages.LoginPage;
import org.keycloak.testsuite.pages.OAuthGrantPage;
import org.keycloak.testsuite.rule.KeycloakRule;
import org.keycloak.testsuite.rule.WebResource;
import org.keycloak.testsuite.rule.WebRule;
import org.openqa.selenium.WebDriver;
/**
* @author <a href="mailto:vrockai@redhat.com">Viliam Rockai</a>
*/
public class OAuthGrantTest {
@ClassRule
public static KeycloakRule keycloakRule = new KeycloakRule();
@BeforeClass
public static void before() {
keycloakRule.deployServlet("grant", "/grant", OAuthGrantServlet.class);
}
@Rule
public WebRule webRule = new WebRule(this);
@WebResource
protected WebDriver driver;
@WebResource
protected OAuthClient oauth;
@WebResource
protected LoginPage loginPage;
@WebResource
protected OAuthGrantPage grantPage;
private static String GRANT_APP_URL = "http://localhost:8081/grant/";
private static String ACCESS_GRANTED = "Access rights granted.";
private static String ACCESS_NOT_GRANTED = "Access rights not granted.";
private static String ROLE_USER = "Have User privileges";
private static String ROLE_CUSTOMER = "Have Customer User privileges";
private static String GRANT_ROLE = "user";
private static String GRANT_APP = "test-app";
private static String GRANT_APP_ROLE = "customer-user";
@Test
public void oauthGrantAcceptTest() throws IOException {
driver.navigate().to(GRANT_APP_URL);
Assert.assertFalse(driver.getPageSource().contains(ACCESS_GRANTED));
Assert.assertFalse(driver.getPageSource().contains(ACCESS_NOT_GRANTED));
loginPage.isCurrent();
loginPage.login("test-user@localhost", "password");
grantPage.assertCurrent();
Assert.assertTrue(driver.getPageSource().contains(ROLE_USER));
Assert.assertTrue(driver.getPageSource().contains(ROLE_CUSTOMER));
grantPage.accept();
Assert.assertTrue(driver.getPageSource().contains(ACCESS_GRANTED));
Assert.assertFalse(driver.getPageSource().contains(ACCESS_NOT_GRANTED));
Assert.assertTrue(driver.getPageSource().contains("Role:"+ GRANT_ROLE +"."));
Assert.assertTrue(driver.getPageSource().contains("App:"+ GRANT_APP +";"+ GRANT_APP_ROLE +"."));
}
@Test
public void oauthGrantCancelTest() throws IOException {
driver.navigate().to(GRANT_APP_URL);
Assert.assertFalse(driver.getPageSource().contains(ACCESS_GRANTED));
Assert.assertFalse(driver.getPageSource().contains(ACCESS_NOT_GRANTED));
loginPage.isCurrent();
loginPage.login("test-user@localhost", "password");
grantPage.assertCurrent();
Assert.assertTrue(driver.getPageSource().contains(ROLE_USER));
Assert.assertTrue(driver.getPageSource().contains(ROLE_CUSTOMER));
grantPage.cancel();
Assert.assertFalse(driver.getPageSource().contains(ACCESS_GRANTED));
Assert.assertTrue(driver.getPageSource().contains(ACCESS_NOT_GRANTED));
}
}

View file

@ -0,0 +1,55 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.keycloak.testsuite.pages;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class OAuthGrantPage extends Page {
@FindBy(css = "input[name=\"accept\"]")
private WebElement acceptButton;
@FindBy(css = "input[name=\"cancel\"]")
private WebElement cancelButton;
public void accept(){
acceptButton.click();
}
public void cancel(){
cancelButton.click();
}
@Override
public boolean isCurrent() {
return driver.getTitle().equals("OAuth Grant");
}
@Override
void open() {
}
}

View file

@ -24,6 +24,14 @@
{ "type" : "password",
"value" : "password" }
]
},
{
"username" : "third-party",
"enabled": true,
"credentials" : [
{ "type" : "password",
"value" : "password" }
]
}
],
"roles": [
@ -40,6 +48,16 @@
{
"username": "test-user@localhost",
"roles": ["user"]
},
{
"username": "third-party",
"roles": ["KEYCLOAK_IDENTITY_REQUESTER"]
}
],
"scopeMappings": [
{
"username": "third-party",
"roles": ["user"]
}
],
"applications": [
@ -53,6 +71,24 @@
"type": "password",
"value": "password"
}
],
"roles": [
{
"name": "customer-user",
"description": "Have Customer User privileges"
}
],
"roleMappings": [
{
"username": "test-user@localhost",
"roles": ["customer-user"]
}
],
"scopeMappings": [
{
"username": "third-party",
"roles": ["customer-user"]
}
]
}
]