Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
</section>
<section class="info-area">
<h3>Info area</h3>
<p>Already have an account? <a href="<%=application.getContextPath()%>/saas/saas-login.jsp">Log in</a>.</p>
<p>Already have an account? <a href="<%=application.getContextPath()%>/rest/saas/loginPage.html">Log in</a>.</p>
<ul>
<li><strong>Domain:</strong> 10.0.0.1</li>
<li><strong>Zone:</strong> Live</li>
Expand Down
7 changes: 7 additions & 0 deletions sdk-html/src/main/java/org/keycloak/sdk/LoginBean.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class LoginBean {

private String socialLoginUrl;

private String registrationUrl;

private String username;

private List<RequiredCredential> requiredCredentials;
Expand Down Expand Up @@ -61,6 +63,7 @@ public void init() {
}

loginAction = ((URI) request.getAttribute("KEYCLOAK_LOGIN_ACTION")).toString();
registrationUrl = ((URI) request.getAttribute("KEYCLOAK_REGISTRATION_PAGE")).toString();
socialLoginUrl = ((URI) request.getAttribute("KEYCLOAK_SOCIAL_LOGIN")).toString();

username = (String) request.getAttribute("username");
Expand Down Expand Up @@ -119,6 +122,10 @@ public String getThemeUrl() {
return themeUrl;
}

public String getRegistrationUrl() {
return registrationUrl;
}

public String getUsername() {
return username;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<ui:define name="info">
<h:panelGroup rendered="#{login.registrationAllowed}">
<p>No account? <a href="saas-register.html">Register</a>.</p>
<p>No account? <a href="#{login.registrationUrl}">Register</a>.</p>
</h:panelGroup>
</ui:define>
</ui:composition>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.keycloak.services.models.RoleModel;
import org.keycloak.services.models.UserModel;

import java.net.URI;

public abstract class AbstractLoginService {

@Context
Expand Down Expand Up @@ -86,6 +88,7 @@ protected void forwardToLoginForm(String redirect, String clientId, String scope
request.setAttribute(RealmModel.class.getName(), realm);
request.setAttribute("KEYCLOAK_LOGIN_ACTION", TokenService.processLoginUrl(uriInfo).build(realm.getId()));
request.setAttribute("KEYCLOAK_SOCIAL_LOGIN", SocialService.redirectToProviderAuthUrl(uriInfo).build(realm.getId()));
request.setAttribute("KEYCLOAK_REGISTRATION_PAGE", URI.create("not-implemented-yet"));

// RESTEASY eats the form data, so we send via an attribute
request.setAttribute("redirect_uri", redirect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public void setDisplayName(String displayName) {
public Response whoAmI(final @Context HttpHeaders headers) {
return new Transaction() {
@Override
public Response callImpl()
{
public Response callImpl() {
logger.info("WHOAMI start.");
RealmManager realmManager = new RealmManager(session);
RealmModel realm = realmManager.defaultRealm();
Expand All @@ -115,8 +114,7 @@ public Response callImpl()
public String isLoggedIn(final @Context HttpHeaders headers) {
return new Transaction() {
@Override
public String callImpl()
{
public String callImpl() {
logger.info("WHOAMI Javascript start.");
RealmManager realmManager = new RealmManager(session);
RealmModel realm = realmManager.defaultRealm();
Expand Down Expand Up @@ -145,24 +143,38 @@ public static UriBuilder saasCookiePath(UriInfo uriInfo) {

@Path("admin/realms")
public RealmsAdminResource getRealmsAdmin(@Context final HttpHeaders headers) {
return new Transaction(false) {
@Override
protected RealmsAdminResource callImpl() {
RealmManager realmManager = new RealmManager(session);
RealmModel saasRealm = realmManager.defaultRealm();
if (saasRealm == null) throw new NotFoundException();
UserModel admin = authManager.authenticateSaasIdentity(saasRealm, uriInfo, headers);
if (admin == null) {
throw new NotAuthorizedException("Bearer");
}
RoleModel creatorRole = saasRealm.getRole(SaasService.REALM_CREATOR_ROLE);
if (!saasRealm.hasRole(admin, creatorRole)) {
logger.warn("not a Realm creator");
throw new NotAuthorizedException("Bearer");
}
return new RealmsAdminResource(admin);
}
}.call();
return new Transaction(false) {
@Override
protected RealmsAdminResource callImpl() {
RealmManager realmManager = new RealmManager(session);
RealmModel saasRealm = realmManager.defaultRealm();
if (saasRealm == null) throw new NotFoundException();
UserModel admin = authManager.authenticateSaasIdentity(saasRealm, uriInfo, headers);
if (admin == null) {
throw new NotAuthorizedException("Bearer");
}
RoleModel creatorRole = saasRealm.getRole(SaasService.REALM_CREATOR_ROLE);
if (!saasRealm.hasRole(admin, creatorRole)) {
logger.warn("not a Realm creator");
throw new NotAuthorizedException("Bearer");
}
return new RealmsAdminResource(admin);
}
}.call();
}

@Path("loginPage.html")
@GET
public void loginPage() {
new Transaction() {
@Override
protected void runImpl() {
RealmManager realmManager = new RealmManager(session);
RealmModel realm = realmManager.defaultRealm();
authManager.expireSaasIdentityCookie(uriInfo);
forwardToLoginForm(realm);
}
}.run();
}

@Path("logout")
Expand All @@ -171,12 +183,15 @@ public void logout() {
new Transaction() {
@Override
protected void runImpl() {
RealmManager realmManager = new RealmManager(session);
RealmModel realm = realmManager.defaultRealm();
authManager.expireSaasIdentityCookie(uriInfo);
request.forward(saasLoginPath);
forwardToLoginForm(realm);
}
}.run();
}


@Path("logout-cookie")
@GET
public void logoutCookie() {
Expand All @@ -189,6 +204,18 @@ protected void runImpl() {
}.run();
}

public final static String loginFormPath = "/sdk/login.xhtml";
protected void forwardToLoginForm(RealmModel realm) {
request.setAttribute(RealmModel.class.getName(), realm);
URI action = uriInfo.getBaseUriBuilder().path(SaasService.class).path(SaasService.class, "processLogin").build();
URI register = contextRoot(uriInfo).path(saasRegisterPath).build();
request.setAttribute("KEYCLOAK_LOGIN_ACTION", action);
request.setAttribute("KEYCLOAK_REGISTRATION_PAGE", register);
request.setAttribute("KEYCLOAK_SOCIAL_LOGIN", SocialService.redirectToProviderAuthUrl(uriInfo).build(realm.getId()));
request.forward(loginFormPath);
}



@Path("login")
@POST
Expand All @@ -210,28 +237,28 @@ protected Response callImpl() {
if (user == null) {
logger.info("Not Authenticated! Incorrect user name");
request.setAttribute("KEYCLOAK_LOGIN_ERROR_MESSAGE", "Incorrect user name.");
request.forward(saasLoginPath);
forwardToLoginForm(realm);
return null;
}
if (!user.isEnabled()) {
logger.info("NAccount is disabled, contact admin.");
request.setAttribute("KEYCLOAK_LOGIN_ERROR_MESSAGE", "Account is disabled, contact admin.");
request.forward(saasLoginPath);
forwardToLoginForm(realm);
return null;
}

boolean authenticated = authManager.authenticateForm(realm, user, formData);
if (!authenticated) {
logger.info("Not Authenticated! Invalid credentials");
request.setAttribute("KEYCLOAK_LOGIN_ERROR_MESSAGE", "Invalid credentials.");
request.forward(saasLoginPath);
forwardToLoginForm(realm);
return null;
}

NewCookie cookie = authManager.createSaasIdentityCookie(realm, user, uriInfo);
return Response.status(302)
.cookie(cookie)
.location(contextRoot(uriInfo).path(adminPath).build()).build();
.cookie(cookie)
.location(contextRoot(uriInfo).path(adminPath).build()).build();
}
}.call();
}
Expand Down