/services/src/main/java/org/keycloak/services/resources/TokenService.java

https://github.com/vibe13/keycloak · Java · 1153 lines · 863 code · 168 blank · 122 comment · 165 complexity · fde9651a454728d5dd102e60f6a4c54b MD5 · raw file

Large files are truncated click here to view the full file

  1. package org.keycloak.services.resources;
  2. import org.jboss.logging.Logger;
  3. import org.jboss.resteasy.annotations.cache.NoCache;
  4. import org.jboss.resteasy.specimpl.MultivaluedMapImpl;
  5. import org.jboss.resteasy.spi.BadRequestException;
  6. import org.jboss.resteasy.spi.HttpRequest;
  7. import org.jboss.resteasy.spi.HttpResponse;
  8. import org.jboss.resteasy.spi.NotAcceptableException;
  9. import org.jboss.resteasy.spi.ResteasyProviderFactory;
  10. import org.jboss.resteasy.spi.UnauthorizedException;
  11. import org.keycloak.OAuth2Constants;
  12. import org.keycloak.OAuthErrorException;
  13. import org.keycloak.audit.Audit;
  14. import org.keycloak.audit.Details;
  15. import org.keycloak.audit.Errors;
  16. import org.keycloak.audit.EventType;
  17. import org.keycloak.authentication.AuthenticationProviderException;
  18. import org.keycloak.authentication.AuthenticationProviderManager;
  19. import org.keycloak.login.LoginFormsProvider;
  20. import org.keycloak.models.ApplicationModel;
  21. import org.keycloak.models.ClientModel;
  22. import org.keycloak.models.ClientSessionModel;
  23. import org.keycloak.models.Constants;
  24. import org.keycloak.models.KeycloakSession;
  25. import org.keycloak.models.RealmModel;
  26. import org.keycloak.models.RequiredCredentialModel;
  27. import org.keycloak.models.UserCredentialModel;
  28. import org.keycloak.models.UserModel;
  29. import org.keycloak.models.UserSessionModel;
  30. import org.keycloak.models.utils.KeycloakModelUtils;
  31. import org.keycloak.representations.AccessToken;
  32. import org.keycloak.representations.AccessTokenResponse;
  33. import org.keycloak.representations.idm.CredentialRepresentation;
  34. import org.keycloak.services.ClientConnection;
  35. import org.keycloak.services.managers.AccessCode;
  36. import org.keycloak.services.managers.AuthenticationManager;
  37. import org.keycloak.services.managers.AuthenticationManager.AuthenticationStatus;
  38. import org.keycloak.services.managers.ResourceAdminManager;
  39. import org.keycloak.services.managers.TokenManager;
  40. import org.keycloak.services.messages.Messages;
  41. import org.keycloak.services.resources.flows.Flows;
  42. import org.keycloak.services.resources.flows.OAuthFlows;
  43. import org.keycloak.services.resources.flows.Urls;
  44. import org.keycloak.services.validation.Validation;
  45. import org.keycloak.util.Base64Url;
  46. import org.keycloak.util.BasicAuthHelper;
  47. import javax.ws.rs.Consumes;
  48. import javax.ws.rs.GET;
  49. import javax.ws.rs.HeaderParam;
  50. import javax.ws.rs.OPTIONS;
  51. import javax.ws.rs.POST;
  52. import javax.ws.rs.Path;
  53. import javax.ws.rs.Produces;
  54. import javax.ws.rs.QueryParam;
  55. import javax.ws.rs.core.Context;
  56. import javax.ws.rs.core.HttpHeaders;
  57. import javax.ws.rs.core.MediaType;
  58. import javax.ws.rs.core.MultivaluedMap;
  59. import javax.ws.rs.core.Response;
  60. import javax.ws.rs.core.SecurityContext;
  61. import javax.ws.rs.core.UriBuilder;
  62. import javax.ws.rs.core.UriInfo;
  63. import javax.ws.rs.ext.Providers;
  64. import java.net.URI;
  65. import java.util.HashMap;
  66. import java.util.HashSet;
  67. import java.util.LinkedList;
  68. import java.util.List;
  69. import java.util.Map;
  70. import java.util.Set;
  71. /**
  72. * Resource class for the oauth/openid connect token service
  73. *
  74. * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
  75. * @version $Revision: 1 $
  76. */
  77. public class TokenService {
  78. protected static final Logger logger = Logger.getLogger(TokenService.class);
  79. protected RealmModel realm;
  80. protected TokenManager tokenManager;
  81. private Audit audit;
  82. protected AuthenticationManager authManager;
  83. @Context
  84. protected Providers providers;
  85. @Context
  86. protected SecurityContext securityContext;
  87. @Context
  88. protected UriInfo uriInfo;
  89. @Context
  90. protected HttpHeaders headers;
  91. @Context
  92. protected HttpRequest request;
  93. @Context
  94. protected HttpResponse response;
  95. @Context
  96. protected KeycloakSession session;
  97. @Context
  98. protected ClientConnection clientConnection;
  99. /*
  100. @Context
  101. protected ResourceContext resourceContext;
  102. */
  103. private ResourceAdminManager resourceAdminManager = new ResourceAdminManager();
  104. public TokenService(RealmModel realm, TokenManager tokenManager, Audit audit, AuthenticationManager authManager) {
  105. this.realm = realm;
  106. this.tokenManager = tokenManager;
  107. this.audit = audit;
  108. this.authManager = authManager;
  109. }
  110. public static UriBuilder tokenServiceBaseUrl(UriInfo uriInfo) {
  111. UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
  112. return tokenServiceBaseUrl(baseUriBuilder);
  113. }
  114. public static UriBuilder tokenServiceBaseUrl(UriBuilder baseUriBuilder) {
  115. return baseUriBuilder.path(RealmsResource.class).path(RealmsResource.class, "getTokenService");
  116. }
  117. public static UriBuilder accessCodeToTokenUrl(UriInfo uriInfo) {
  118. UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
  119. return accessCodeToTokenUrl(baseUriBuilder);
  120. }
  121. public static UriBuilder accessCodeToTokenUrl(UriBuilder baseUriBuilder) {
  122. UriBuilder uriBuilder = tokenServiceBaseUrl(baseUriBuilder);
  123. return uriBuilder.path(TokenService.class, "accessCodeToToken");
  124. }
  125. public static UriBuilder grantAccessTokenUrl(UriInfo uriInfo) {
  126. UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
  127. return grantAccessTokenUrl(baseUriBuilder);
  128. }
  129. public static UriBuilder grantAccessTokenUrl(UriBuilder baseUriBuilder) {
  130. UriBuilder uriBuilder = tokenServiceBaseUrl(baseUriBuilder);
  131. return uriBuilder.path(TokenService.class, "grantAccessToken");
  132. }
  133. public static UriBuilder loginPageUrl(UriInfo uriInfo) {
  134. UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
  135. return loginPageUrl(baseUriBuilder);
  136. }
  137. public static UriBuilder loginPageUrl(UriBuilder baseUriBuilder) {
  138. UriBuilder uriBuilder = tokenServiceBaseUrl(baseUriBuilder);
  139. return uriBuilder.path(TokenService.class, "loginPage");
  140. }
  141. public static UriBuilder logoutUrl(UriInfo uriInfo) {
  142. UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
  143. return logoutUrl(baseUriBuilder);
  144. }
  145. public static UriBuilder logoutUrl(UriBuilder baseUriBuilder) {
  146. UriBuilder uriBuilder = tokenServiceBaseUrl(baseUriBuilder);
  147. return uriBuilder.path(TokenService.class, "logout");
  148. }
  149. public static UriBuilder processLoginUrl(UriInfo uriInfo) {
  150. UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
  151. return processLoginUrl(baseUriBuilder);
  152. }
  153. public static UriBuilder processLoginUrl(UriBuilder baseUriBuilder) {
  154. UriBuilder uriBuilder = tokenServiceBaseUrl(baseUriBuilder);
  155. return uriBuilder.path(TokenService.class, "processLogin");
  156. }
  157. public static UriBuilder processOAuthUrl(UriInfo uriInfo) {
  158. UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
  159. return processOAuthUrl(baseUriBuilder);
  160. }
  161. public static UriBuilder processOAuthUrl(UriBuilder baseUriBuilder) {
  162. UriBuilder uriBuilder = tokenServiceBaseUrl(baseUriBuilder);
  163. return uriBuilder.path(TokenService.class, "processOAuth");
  164. }
  165. public static UriBuilder refreshUrl(UriBuilder baseUriBuilder) {
  166. UriBuilder uriBuilder = tokenServiceBaseUrl(baseUriBuilder);
  167. return uriBuilder.path(TokenService.class, "refreshAccessToken");
  168. }
  169. /**
  170. * Direct grant REST invocation. One stop call to obtain an access token.
  171. *
  172. * If the client is a confidential client
  173. * you must include the client-id (application name or oauth client name) and secret in an Basic Auth Authorization header.
  174. *
  175. * If the client is a public client, then you must include a "client_id" form parameter with the app's or oauth client's name.
  176. *
  177. * The realm must be configured to allow these types of auth requests. (Direct Grant API in admin console Settings page)
  178. *
  179. *
  180. * @See <a href="http://tools.ietf.org/html/rfc6749#section-4.3">http://tools.ietf.org/html/rfc6749#section-4.3</a>
  181. *
  182. * @param authorizationHeader
  183. * @param form
  184. * @return @see org.keycloak.representations.AccessTokenResponse
  185. */
  186. @Path("grants/access")
  187. @POST
  188. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  189. @Produces(MediaType.APPLICATION_JSON)
  190. public Response grantAccessToken(final @HeaderParam(HttpHeaders.AUTHORIZATION) String authorizationHeader,
  191. final MultivaluedMap<String, String> form) {
  192. if (!checkSsl()) {
  193. return createError("https_required", "HTTPS required", Response.Status.FORBIDDEN);
  194. }
  195. if (!realm.isPasswordCredentialGrantAllowed()) {
  196. return createError("not_enabled", "Resource Owner Password Credentials Grant not enabled", Response.Status.FORBIDDEN);
  197. }
  198. audit.event(EventType.LOGIN).detail(Details.AUTH_METHOD, "oauth_credentials").detail(Details.RESPONSE_TYPE, "token");
  199. String username = form.getFirst(AuthenticationManager.FORM_USERNAME);
  200. if (username == null) {
  201. audit.error(Errors.USERNAME_MISSING);
  202. throw new UnauthorizedException("No username");
  203. }
  204. audit.detail(Details.USERNAME, username);
  205. UserModel user = session.users().getUserByUsername(username, realm);
  206. if (user != null) audit.user(user);
  207. ClientModel client = authorizeClient(authorizationHeader, form, audit);
  208. if ( (client instanceof ApplicationModel) && ((ApplicationModel)client).isBearerOnly()) {
  209. audit.error(Errors.NOT_ALLOWED);
  210. return createError("not_allowed", "Bearer-only applications are not allowed to invoke grants/access", Response.Status.FORBIDDEN);
  211. }
  212. if (!realm.isEnabled()) {
  213. audit.error(Errors.REALM_DISABLED);
  214. return createError("realm_disabled", "Realm is disabled", Response.Status.UNAUTHORIZED);
  215. }
  216. AuthenticationStatus authenticationStatus = authManager.authenticateForm(session, clientConnection, realm, form);
  217. Map<String, String> err;
  218. switch (authenticationStatus) {
  219. case SUCCESS:
  220. break;
  221. case ACCOUNT_TEMPORARILY_DISABLED:
  222. case ACTIONS_REQUIRED:
  223. err = new HashMap<String, String>();
  224. err.put(OAuth2Constants.ERROR, "invalid_grant");
  225. err.put(OAuth2Constants.ERROR_DESCRIPTION, "AccountProvider temporarily disabled");
  226. audit.error(Errors.USER_TEMPORARILY_DISABLED);
  227. return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(err)
  228. .build();
  229. case ACCOUNT_DISABLED:
  230. err = new HashMap<String, String>();
  231. err.put(OAuth2Constants.ERROR, "invalid_grant");
  232. err.put(OAuth2Constants.ERROR_DESCRIPTION, "AccountProvider disabled");
  233. audit.error(Errors.USER_DISABLED);
  234. return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(err)
  235. .build();
  236. default:
  237. err = new HashMap<String, String>();
  238. err.put(OAuth2Constants.ERROR, "invalid_grant");
  239. err.put(OAuth2Constants.ERROR_DESCRIPTION, "Invalid user credentials");
  240. audit.error(Errors.INVALID_USER_CREDENTIALS);
  241. return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(err)
  242. .build();
  243. }
  244. String scope = form.getFirst(OAuth2Constants.SCOPE);
  245. UserSessionModel userSession = session.sessions().createUserSession(realm, user, username, clientConnection.getRemoteAddr(), "oauth_credentials", false);
  246. audit.session(userSession);
  247. AccessTokenResponse res = tokenManager.responseBuilder(realm, client, audit)
  248. .generateAccessToken(scope, client, user, userSession)
  249. .generateRefreshToken()
  250. .generateIDToken()
  251. .build();
  252. audit.success();
  253. return Response.ok(res, MediaType.APPLICATION_JSON_TYPE).build();
  254. }
  255. /**
  256. * URL for making refresh token requests.
  257. *
  258. * @See <a href="http://tools.ietf.org/html/rfc6749#section-6">http://tools.ietf.org/html/rfc6749#section-6</a>
  259. *
  260. * If the client is a confidential client
  261. * you must include the client-id (application name or oauth client name) and secret in an Basic Auth Authorization header.
  262. *
  263. * If the client is a public client, then you must include a "client_id" form parameter with the app's or oauth client's name.
  264. *
  265. * @param authorizationHeader
  266. * @param form
  267. * @return
  268. */
  269. @Path("refresh")
  270. @POST
  271. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  272. @Produces(MediaType.APPLICATION_JSON)
  273. public Response refreshAccessToken(final @HeaderParam(HttpHeaders.AUTHORIZATION) String authorizationHeader,
  274. final MultivaluedMap<String, String> form) {
  275. logger.info("--> refreshAccessToken");
  276. if (!checkSsl()) {
  277. throw new NotAcceptableException("HTTPS required");
  278. }
  279. audit.event(EventType.REFRESH_TOKEN);
  280. ClientModel client = authorizeClient(authorizationHeader, form, audit);
  281. String refreshToken = form.getFirst(OAuth2Constants.REFRESH_TOKEN);
  282. AccessToken accessToken;
  283. try {
  284. accessToken = tokenManager.refreshAccessToken(session, uriInfo, realm, client, refreshToken, audit);
  285. } catch (OAuthErrorException e) {
  286. Map<String, String> error = new HashMap<String, String>();
  287. error.put(OAuth2Constants.ERROR, e.getError());
  288. if (e.getDescription() != null) error.put(OAuth2Constants.ERROR_DESCRIPTION, e.getDescription());
  289. audit.error(Errors.INVALID_TOKEN);
  290. logger.error("OAuth Error", e);
  291. return Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build();
  292. }
  293. AccessTokenResponse res = tokenManager.responseBuilder(realm, client, audit)
  294. .accessToken(accessToken)
  295. .generateIDToken()
  296. .generateRefreshToken().build();
  297. audit.success();
  298. return Cors.add(request, Response.ok(res, MediaType.APPLICATION_JSON_TYPE)).auth().allowedOrigins(client).allowedMethods("POST").exposedHeaders(Cors.ACCESS_CONTROL_ALLOW_METHODS).build();
  299. }
  300. /**
  301. * URL called after login page. YOU SHOULD NEVER INVOKE THIS DIRECTLY!
  302. *
  303. * @param clientId
  304. * @param scopeParam
  305. * @param state
  306. * @param redirect
  307. * @param formData
  308. * @return
  309. */
  310. @Path("auth/request/login")
  311. @POST
  312. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  313. public Response processLogin(@QueryParam("client_id") final String clientId, @QueryParam("scope") final String scopeParam,
  314. @QueryParam("state") final String state, @QueryParam("redirect_uri") String redirect,
  315. final MultivaluedMap<String, String> formData) {
  316. logger.debug("TokenService.processLogin");
  317. String username = formData.getFirst(AuthenticationManager.FORM_USERNAME);
  318. String rememberMe = formData.getFirst("rememberMe");
  319. boolean remember = rememberMe != null && rememberMe.equalsIgnoreCase("on");
  320. logger.debug("*** Remember me: " + remember);
  321. audit.event(EventType.LOGIN).client(clientId)
  322. .detail(Details.REDIRECT_URI, redirect)
  323. .detail(Details.RESPONSE_TYPE, "code")
  324. .detail(Details.AUTH_METHOD, "form")
  325. .detail(Details.USERNAME, username);
  326. if (remember) {
  327. audit.detail(Details.REMEMBER_ME, "true");
  328. }
  329. OAuthFlows oauth = Flows.oauth(session, realm, request, uriInfo, authManager, tokenManager);
  330. if (!checkSsl()) {
  331. return oauth.forwardToSecurityFailure("HTTPS required");
  332. }
  333. if (!realm.isEnabled()) {
  334. audit.error(Errors.REALM_DISABLED);
  335. return oauth.forwardToSecurityFailure("Realm not enabled.");
  336. }
  337. ClientModel client = realm.findClient(clientId);
  338. if (client == null) {
  339. audit.error(Errors.CLIENT_NOT_FOUND);
  340. return oauth.forwardToSecurityFailure("Unknown login requester.");
  341. }
  342. if (!client.isEnabled()) {
  343. audit.error(Errors.CLIENT_NOT_FOUND);
  344. return oauth.forwardToSecurityFailure("Login requester not enabled.");
  345. }
  346. redirect = verifyRedirectUri(uriInfo, redirect, realm, client);
  347. if (redirect == null) {
  348. audit.error(Errors.INVALID_REDIRECT_URI);
  349. return oauth.forwardToSecurityFailure("Invalid redirect_uri.");
  350. }
  351. if (formData.containsKey("cancel")) {
  352. audit.error(Errors.REJECTED_BY_USER);
  353. return oauth.redirectError(client, "access_denied", state, redirect);
  354. }
  355. AuthenticationStatus status = authManager.authenticateForm(session, clientConnection, realm, formData);
  356. if (remember) {
  357. authManager.createRememberMeCookie(realm, uriInfo);
  358. } else {
  359. authManager.expireRememberMeCookie(realm, uriInfo);
  360. }
  361. UserModel user = KeycloakModelUtils.findUserByNameOrEmail(session, realm, username);
  362. if (user != null) {
  363. audit.user(user);
  364. }
  365. switch (status) {
  366. case SUCCESS:
  367. case ACTIONS_REQUIRED:
  368. UserSessionModel userSession = session.sessions().createUserSession(realm, user, username, clientConnection.getRemoteAddr(), "form", remember);
  369. audit.session(userSession);
  370. return oauth.processAccessCode(scopeParam, state, redirect, client, user, userSession, audit);
  371. case ACCOUNT_TEMPORARILY_DISABLED:
  372. audit.error(Errors.USER_TEMPORARILY_DISABLED);
  373. return Flows.forms(this.session, realm, uriInfo).setError(Messages.ACCOUNT_TEMPORARILY_DISABLED).setFormData(formData).createLogin();
  374. case ACCOUNT_DISABLED:
  375. audit.error(Errors.USER_DISABLED);
  376. return Flows.forms(this.session, realm, uriInfo).setError(Messages.ACCOUNT_DISABLED).setFormData(formData).createLogin();
  377. case MISSING_TOTP:
  378. return Flows.forms(this.session, realm, uriInfo).setFormData(formData).createLoginTotp();
  379. case INVALID_USER:
  380. audit.error(Errors.USER_NOT_FOUND);
  381. return Flows.forms(this.session, realm, uriInfo).setError(Messages.INVALID_USER).setFormData(formData).createLogin();
  382. default:
  383. audit.error(Errors.INVALID_USER_CREDENTIALS);
  384. return Flows.forms(this.session, realm, uriInfo).setError(Messages.INVALID_USER).setFormData(formData).createLogin();
  385. }
  386. }
  387. @Path("auth/request/login-actions")
  388. public RequiredActionsService getRequiredActionsService() {
  389. RequiredActionsService service = new RequiredActionsService(realm, tokenManager, audit);
  390. ResteasyProviderFactory.getInstance().injectProperties(service);
  391. //resourceContext.initResource(service);
  392. return service;
  393. }
  394. /**
  395. * Registration
  396. *
  397. * @param clientId
  398. * @param scopeParam
  399. * @param state
  400. * @param redirect
  401. * @param formData
  402. * @return
  403. */
  404. @Path("registrations")
  405. @POST
  406. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  407. public Response processRegister(@QueryParam("client_id") final String clientId,
  408. @QueryParam("scope") final String scopeParam, @QueryParam("state") final String state,
  409. @QueryParam("redirect_uri") String redirect, final MultivaluedMap<String, String> formData) {
  410. String username = formData.getFirst("username");
  411. String email = formData.getFirst("email");
  412. audit.event(EventType.REGISTER).client(clientId)
  413. .detail(Details.REDIRECT_URI, redirect)
  414. .detail(Details.RESPONSE_TYPE, "code")
  415. .detail(Details.USERNAME, username)
  416. .detail(Details.EMAIL, email)
  417. .detail(Details.REGISTER_METHOD, "form");
  418. OAuthFlows oauth = Flows.oauth(session, realm, request, uriInfo, authManager, tokenManager);
  419. if (!realm.isEnabled()) {
  420. logger.warn("Realm not enabled");
  421. audit.error(Errors.REALM_DISABLED);
  422. return oauth.forwardToSecurityFailure("Realm not enabled");
  423. }
  424. ClientModel client = realm.findClient(clientId);
  425. if (client == null) {
  426. logger.warn("Unknown login requester.");
  427. audit.error(Errors.CLIENT_NOT_FOUND);
  428. return oauth.forwardToSecurityFailure("Unknown login requester.");
  429. }
  430. if (!client.isEnabled()) {
  431. logger.warn("Login requester not enabled.");
  432. audit.error(Errors.CLIENT_DISABLED);
  433. return oauth.forwardToSecurityFailure("Login requester not enabled.");
  434. }
  435. redirect = verifyRedirectUri(uriInfo, redirect, realm, client);
  436. if (redirect == null) {
  437. audit.error(Errors.INVALID_REDIRECT_URI);
  438. return oauth.forwardToSecurityFailure("Invalid redirect_uri.");
  439. }
  440. if (!realm.isRegistrationAllowed()) {
  441. logger.warn("Registration not allowed");
  442. audit.error(Errors.REGISTRATION_DISABLED);
  443. return oauth.forwardToSecurityFailure("Registration not allowed");
  444. }
  445. List<String> requiredCredentialTypes = new LinkedList<String>();
  446. for (RequiredCredentialModel m : realm.getRequiredCredentials()) {
  447. requiredCredentialTypes.add(m.getType());
  448. }
  449. // Validate here, so user is not created if password doesn't validate to passwordPolicy of current realm
  450. String error = Validation.validateRegistrationForm(formData, requiredCredentialTypes);
  451. if (error == null) {
  452. error = Validation.validatePassword(formData, realm.getPasswordPolicy());
  453. }
  454. if (error != null) {
  455. audit.error(Errors.INVALID_REGISTRATION);
  456. return Flows.forms(session, realm, uriInfo).setError(error).setFormData(formData).createRegistration();
  457. }
  458. AuthenticationProviderManager authenticationProviderManager = AuthenticationProviderManager.getManager(realm, session);
  459. // Validate that user with this username doesn't exist in realm or any authentication provider
  460. if (session.users().getUserByUsername(username, realm) != null || authenticationProviderManager.getUser(username) != null) {
  461. audit.error(Errors.USERNAME_IN_USE);
  462. return Flows.forms(session, realm, uriInfo).setError(Messages.USERNAME_EXISTS).setFormData(formData).createRegistration();
  463. }
  464. UserModel user = session.users().addUser(realm, username);
  465. user.setEnabled(true);
  466. user.setFirstName(formData.getFirst("firstName"));
  467. user.setLastName(formData.getFirst("lastName"));
  468. user.setEmail(email);
  469. if (requiredCredentialTypes.contains(CredentialRepresentation.PASSWORD)) {
  470. UserCredentialModel credentials = new UserCredentialModel();
  471. credentials.setType(CredentialRepresentation.PASSWORD);
  472. credentials.setValue(formData.getFirst("password"));
  473. boolean passwordUpdateSuccessful;
  474. String passwordUpdateError;
  475. try {
  476. passwordUpdateSuccessful = AuthenticationProviderManager.getManager(realm, session).updatePassword(user, formData.getFirst("password"));
  477. passwordUpdateError = "Password update failed";
  478. } catch (AuthenticationProviderException ape) {
  479. passwordUpdateSuccessful = false;
  480. passwordUpdateError = ape.getMessage();
  481. }
  482. // User already registered, but force him to update password
  483. if (!passwordUpdateSuccessful) {
  484. user.addRequiredAction(UserModel.RequiredAction.UPDATE_PASSWORD);
  485. return Flows.forms(session, realm, uriInfo).setError(passwordUpdateError).createResponse(UserModel.RequiredAction.UPDATE_PASSWORD);
  486. }
  487. }
  488. audit.user(user).success();
  489. audit.reset();
  490. return processLogin(clientId, scopeParam, state, redirect, formData);
  491. }
  492. /**
  493. * CORS preflight path for access code to token
  494. *
  495. * @return
  496. */
  497. @Path("access/codes")
  498. @OPTIONS
  499. @Produces("application/json")
  500. public Response accessCodeToTokenPreflight() {
  501. logger.debugv("cors request from: {0}" , request.getHttpHeaders().getRequestHeaders().getFirst("Origin"));
  502. return Cors.add(request, Response.ok()).auth().preflight().build();
  503. }
  504. /**
  505. * URL invoked by adapter to turn an access code to access token
  506. *
  507. * @See <a href="http://tools.ietf.org/html/rfc6749#section-4.1">http://tools.ietf.org/html/rfc6749#section-4.1</a>
  508. *
  509. * @param authorizationHeader
  510. * @param formData
  511. * @return
  512. */
  513. @Path("access/codes")
  514. @POST
  515. @Produces("application/json")
  516. public Response accessCodeToToken(@HeaderParam(HttpHeaders.AUTHORIZATION) String authorizationHeader, final MultivaluedMap<String, String> formData) {
  517. logger.debug("accessRequest <---");
  518. if (!checkSsl()) {
  519. throw new NotAcceptableException("HTTPS required");
  520. }
  521. audit.event(EventType.CODE_TO_TOKEN);
  522. if (!realm.isEnabled()) {
  523. audit.error(Errors.REALM_DISABLED);
  524. throw new UnauthorizedException("Realm not enabled");
  525. }
  526. String code = formData.getFirst(OAuth2Constants.CODE);
  527. if (code == null) {
  528. Map<String, String> error = new HashMap<String, String>();
  529. error.put(OAuth2Constants.ERROR, "invalid_request");
  530. error.put(OAuth2Constants.ERROR_DESCRIPTION, "code not specified");
  531. audit.error(Errors.INVALID_CODE);
  532. throw new BadRequestException("Code not specified", Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build());
  533. }
  534. AccessCode accessCode = AccessCode.parse(code, session, realm);
  535. if (accessCode == null) {
  536. String[] parts = code.split("\\.");
  537. if (parts.length == 2) {
  538. try {
  539. audit.detail(Details.CODE_ID, new String(Base64Url.decode(parts[1])));
  540. } catch (Throwable t) {
  541. }
  542. }
  543. Map<String, String> res = new HashMap<String, String>();
  544. res.put(OAuth2Constants.ERROR, "invalid_grant");
  545. res.put(OAuth2Constants.ERROR_DESCRIPTION, "Code not found");
  546. audit.error(Errors.INVALID_CODE);
  547. return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(res)
  548. .build();
  549. }
  550. audit.detail(Details.CODE_ID, accessCode.getCodeId());
  551. if (!accessCode.isValid(ClientSessionModel.Action.CODE_TO_TOKEN)) {
  552. Map<String, String> res = new HashMap<String, String>();
  553. res.put(OAuth2Constants.ERROR, "invalid_grant");
  554. res.put(OAuth2Constants.ERROR_DESCRIPTION, "Code is expired");
  555. audit.error(Errors.INVALID_CODE);
  556. return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(res)
  557. .build();
  558. }
  559. accessCode.setAction(null);
  560. audit.user(accessCode.getUser());
  561. audit.session(accessCode.getSessionState());
  562. ClientModel client = authorizeClient(authorizationHeader, formData, audit);
  563. if (!client.getClientId().equals(accessCode.getClient().getClientId())) {
  564. Map<String, String> res = new HashMap<String, String>();
  565. res.put(OAuth2Constants.ERROR, "invalid_grant");
  566. res.put(OAuth2Constants.ERROR_DESCRIPTION, "Auth error");
  567. audit.error(Errors.INVALID_CODE);
  568. return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(res)
  569. .build();
  570. }
  571. UserModel user = session.users().getUserById(accessCode.getUser().getId(), realm);
  572. if (user == null) {
  573. Map<String, String> res = new HashMap<String, String>();
  574. res.put(OAuth2Constants.ERROR, "invalid_grant");
  575. res.put(OAuth2Constants.ERROR_DESCRIPTION, "User not found");
  576. audit.error(Errors.INVALID_CODE);
  577. return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(res)
  578. .build();
  579. }
  580. if (!user.isEnabled()) {
  581. Map<String, String> res = new HashMap<String, String>();
  582. res.put(OAuth2Constants.ERROR, "invalid_grant");
  583. res.put(OAuth2Constants.ERROR_DESCRIPTION, "User disabled");
  584. audit.error(Errors.INVALID_CODE);
  585. return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(res)
  586. .build();
  587. }
  588. UserSessionModel userSession = session.sessions().getUserSession(realm, accessCode.getSessionState());
  589. if (!AuthenticationManager.isSessionValid(realm, userSession)) {
  590. AuthenticationManager.logout(session, realm, userSession, uriInfo);
  591. Map<String, String> res = new HashMap<String, String>();
  592. res.put(OAuth2Constants.ERROR, "invalid_grant");
  593. res.put(OAuth2Constants.ERROR_DESCRIPTION, "Session not active");
  594. audit.error(Errors.INVALID_CODE);
  595. return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(res)
  596. .build();
  597. }
  598. logger.debug("accessRequest SUCCESS");
  599. AccessToken token = tokenManager.createClientAccessToken(accessCode.getRequestedRoles(), realm, client, user, userSession);
  600. try {
  601. tokenManager.verifyAccess(token, realm, client, user);
  602. } catch (OAuthErrorException e) {
  603. Map<String, String> error = new HashMap<String, String>();
  604. error.put(OAuth2Constants.ERROR, e.getError());
  605. if (e.getDescription() != null) error.put(OAuth2Constants.ERROR_DESCRIPTION, e.getDescription());
  606. audit.error(Errors.INVALID_CODE);
  607. return Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build();
  608. }
  609. AccessTokenResponse res = tokenManager.responseBuilder(realm, client, audit)
  610. .accessToken(token)
  611. .generateIDToken()
  612. .generateRefreshToken().build();
  613. audit.success();
  614. return Cors.add(request, Response.ok(res)).auth().allowedOrigins(client).allowedMethods("POST").exposedHeaders(Cors.ACCESS_CONTROL_ALLOW_METHODS).build();
  615. }
  616. protected ClientModel authorizeClient(String authorizationHeader, MultivaluedMap<String, String> formData, Audit audit) {
  617. String client_id;
  618. String clientSecret;
  619. if (authorizationHeader != null) {
  620. String[] usernameSecret = BasicAuthHelper.parseHeader(authorizationHeader);
  621. if (usernameSecret == null) {
  622. throw new UnauthorizedException("Bad Authorization header", Response.status(401).header(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"" + realm.getName() + "\"").build());
  623. }
  624. client_id = usernameSecret[0];
  625. clientSecret = usernameSecret[1];
  626. } else {
  627. logger.info("no authorization header");
  628. client_id = formData.getFirst(OAuth2Constants.CLIENT_ID);
  629. clientSecret = formData.getFirst("client_secret");
  630. }
  631. if (client_id == null) {
  632. Map<String, String> error = new HashMap<String, String>();
  633. error.put(OAuth2Constants.ERROR, "invalid_client");
  634. error.put(OAuth2Constants.ERROR_DESCRIPTION, "Could not find client");
  635. throw new BadRequestException("Could not find client", Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build());
  636. }
  637. audit.client(client_id);
  638. ClientModel client = realm.findClient(client_id);
  639. if (client == null) {
  640. Map<String, String> error = new HashMap<String, String>();
  641. error.put(OAuth2Constants.ERROR, "invalid_client");
  642. error.put(OAuth2Constants.ERROR_DESCRIPTION, "Could not find client");
  643. audit.error(Errors.CLIENT_NOT_FOUND);
  644. throw new BadRequestException("Could not find client", Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build());
  645. }
  646. if (!client.isEnabled()) {
  647. Map<String, String> error = new HashMap<String, String>();
  648. error.put(OAuth2Constants.ERROR, "invalid_client");
  649. error.put(OAuth2Constants.ERROR_DESCRIPTION, "Client is not enabled");
  650. audit.error(Errors.CLIENT_DISABLED);
  651. throw new BadRequestException("Client is not enabled", Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build());
  652. }
  653. if ( (client instanceof ApplicationModel) && ((ApplicationModel)client).isBearerOnly()) {
  654. Map<String, String> error = new HashMap<String, String>();
  655. error.put(OAuth2Constants.ERROR, "invalid_client");
  656. error.put(OAuth2Constants.ERROR_DESCRIPTION, "Bearer-only not allowed");
  657. audit.error(Errors.INVALID_CLIENT);
  658. throw new BadRequestException("Bearer-only not allowed", Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build());
  659. }
  660. if (!client.isPublicClient()) {
  661. if (clientSecret == null || !client.validateSecret(clientSecret)) {
  662. Map<String, String> error = new HashMap<String, String>();
  663. error.put(OAuth2Constants.ERROR, "unauthorized_client");
  664. audit.error(Errors.INVALID_CLIENT_CREDENTIALS);
  665. throw new BadRequestException("Unauthorized Client", Response.status(Response.Status.BAD_REQUEST).entity(error).type("application/json").build());
  666. }
  667. }
  668. return client;
  669. }
  670. /**
  671. * Login page. Must be redirected to from the application or oauth client.
  672. *
  673. * @See <a href="http://tools.ietf.org/html/rfc6749#section-4.1">http://tools.ietf.org/html/rfc6749#section-4.1</a>
  674. *
  675. *
  676. * @param responseType
  677. * @param redirect
  678. * @param clientId
  679. * @param scopeParam
  680. * @param state
  681. * @param prompt
  682. * @return
  683. */
  684. @Path("login")
  685. @GET
  686. public Response loginPage(final @QueryParam("response_type") String responseType,
  687. @QueryParam("redirect_uri") String redirect, final @QueryParam("client_id") String clientId,
  688. final @QueryParam("scope") String scopeParam, final @QueryParam("state") String state, final @QueryParam("prompt") String prompt,
  689. final @QueryParam("login_hint") String loginHint) {
  690. logger.info("TokenService.loginPage");
  691. audit.event(EventType.LOGIN).client(clientId).detail(Details.REDIRECT_URI, redirect).detail(Details.RESPONSE_TYPE, "code");
  692. OAuthFlows oauth = Flows.oauth(session, realm, request, uriInfo, authManager, tokenManager);
  693. if (!checkSsl()) {
  694. return oauth.forwardToSecurityFailure("HTTPS required");
  695. }
  696. if (!realm.isEnabled()) {
  697. logger.warn("Realm not enabled");
  698. audit.error(Errors.REALM_DISABLED);
  699. return oauth.forwardToSecurityFailure("Realm not enabled");
  700. }
  701. ClientModel client = realm.findClient(clientId);
  702. if (client == null) {
  703. logger.warn("Unknown login requester: " + clientId);
  704. audit.error(Errors.CLIENT_NOT_FOUND);
  705. return oauth.forwardToSecurityFailure("Unknown login requester.");
  706. }
  707. if (!client.isEnabled()) {
  708. logger.warn("Login requester not enabled.");
  709. audit.error(Errors.CLIENT_DISABLED);
  710. return oauth.forwardToSecurityFailure("Login requester not enabled.");
  711. }
  712. if ( (client instanceof ApplicationModel) && ((ApplicationModel)client).isBearerOnly()) {
  713. audit.error(Errors.NOT_ALLOWED);
  714. return oauth.forwardToSecurityFailure("Bearer-only applications are not allowed to initiate browser login");
  715. }
  716. if (client.isDirectGrantsOnly()) {
  717. audit.error(Errors.NOT_ALLOWED);
  718. return oauth.forwardToSecurityFailure("direct-grants-only clients are not allowed to initiate browser login");
  719. }
  720. redirect = verifyRedirectUri(uriInfo, redirect, realm, client);
  721. if (redirect == null) {
  722. audit.error(Errors.INVALID_REDIRECT_URI);
  723. return oauth.forwardToSecurityFailure("Invalid redirect_uri.");
  724. }
  725. logger.info("Checking cookie...");
  726. AuthenticationManager.AuthResult authResult = authManager.authenticateIdentityCookie(session, realm, uriInfo, headers);
  727. if (authResult != null) {
  728. UserModel user = authResult.getUser();
  729. UserSessionModel session = authResult.getSession();
  730. logger.debug(user.getUsername() + " already logged in.");
  731. audit.user(user).session(session).detail(Details.AUTH_METHOD, "sso");
  732. return oauth.processAccessCode(scopeParam, state, redirect, client, user, session, audit);
  733. }
  734. if (prompt != null && prompt.equals("none")) {
  735. return oauth.redirectError(client, "access_denied", state, redirect);
  736. }
  737. LoginFormsProvider forms = Flows.forms(session, realm, uriInfo);
  738. if (loginHint != null) {
  739. MultivaluedMap<String, String> formData = new MultivaluedMapImpl<String, String>();
  740. formData.add(AuthenticationManager.FORM_USERNAME, loginHint);
  741. forms.setFormData(formData);
  742. }
  743. return forms.createLogin();
  744. }
  745. /**
  746. * Registration page. Must be redirected to from login page.
  747. *
  748. * @param responseType
  749. * @param redirect
  750. * @param clientId
  751. * @param scopeParam
  752. * @param state
  753. * @return
  754. */
  755. @Path("registrations")
  756. @GET
  757. public Response registerPage(final @QueryParam("response_type") String responseType,
  758. @QueryParam("redirect_uri") String redirect, final @QueryParam("client_id") String clientId,
  759. final @QueryParam("scope") String scopeParam, final @QueryParam("state") String state) {
  760. logger.info("**********registerPage()");
  761. audit.event(EventType.REGISTER).client(clientId).detail(Details.REDIRECT_URI, redirect).detail(Details.RESPONSE_TYPE, "code");
  762. OAuthFlows oauth = Flows.oauth(session, realm, request, uriInfo, authManager, tokenManager);
  763. if (!checkSsl()) {
  764. return oauth.forwardToSecurityFailure("HTTPS required");
  765. }
  766. if (!realm.isEnabled()) {
  767. logger.warn("Realm not enabled");
  768. audit.error(Errors.REALM_DISABLED);
  769. return oauth.forwardToSecurityFailure("Realm not enabled");
  770. }
  771. ClientModel client = realm.findClient(clientId);
  772. if (client == null) {
  773. logger.warn("Unknown login requester.");
  774. audit.error(Errors.CLIENT_NOT_FOUND);
  775. return oauth.forwardToSecurityFailure("Unknown login requester.");
  776. }
  777. if (!client.isEnabled()) {
  778. logger.warn("Login requester not enabled.");
  779. audit.error(Errors.CLIENT_DISABLED);
  780. return oauth.forwardToSecurityFailure("Login requester not enabled.");
  781. }
  782. redirect = verifyRedirectUri(uriInfo, redirect, realm, client);
  783. if (redirect == null) {
  784. audit.error(Errors.INVALID_REDIRECT_URI);
  785. return oauth.forwardToSecurityFailure("Invalid redirect_uri.");
  786. }
  787. if (!realm.isRegistrationAllowed()) {
  788. logger.warn("Registration not allowed");
  789. audit.error(Errors.REGISTRATION_DISABLED);
  790. return oauth.forwardToSecurityFailure("Registration not allowed");
  791. }
  792. authManager.expireIdentityCookie(realm, uriInfo);
  793. return Flows.forms(session, realm, uriInfo).createRegistration();
  794. }
  795. /**
  796. * Logout user session.
  797. *
  798. * @param sessionState
  799. * @param redirectUri
  800. * @return
  801. */
  802. @Path("logout")
  803. @GET
  804. @NoCache
  805. public Response logout(final @QueryParam("session_state") String sessionState, final @QueryParam("redirect_uri") String redirectUri) {
  806. // todo do we care if anybody can trigger this?
  807. audit.event(EventType.LOGOUT);
  808. if (redirectUri != null) {
  809. audit.detail(Details.REDIRECT_URI, redirectUri);
  810. }
  811. if (sessionState != null) {
  812. audit.session(sessionState);
  813. }
  814. // authenticate identity cookie, but ignore an access token timeout as we're logging out anyways.
  815. AuthenticationManager.AuthResult authResult = authManager.authenticateIdentityCookie(session, realm, uriInfo, headers, false);
  816. if (authResult != null) {
  817. logout(authResult.getSession());
  818. } else if (sessionState != null) {
  819. UserSessionModel userSession = session.sessions().getUserSession(realm, sessionState);
  820. if (userSession != null) {
  821. logout(userSession);
  822. } else {
  823. audit.error(Errors.USER_SESSION_NOT_FOUND);
  824. }
  825. } else {
  826. audit.error(Errors.USER_NOT_LOGGED_IN);
  827. }
  828. if (redirectUri != null) {
  829. // todo manage legal redirects
  830. return Response.status(302).location(UriBuilder.fromUri(redirectUri).build()).build();
  831. } else {
  832. return Response.ok().build();
  833. }
  834. }
  835. private void logout(UserSessionModel userSession) {
  836. authManager.logout(session, realm, userSession, uriInfo);
  837. audit.user(userSession.getUser()).session(userSession).success();
  838. }
  839. /**
  840. * OAuth grant page. You should not invoked this directly!
  841. *
  842. * @param formData
  843. * @return
  844. */
  845. @Path("oauth/grant")
  846. @POST
  847. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  848. public Response processOAuth(final MultivaluedMap<String, String> formData) {
  849. audit.event(EventType.LOGIN).detail(Details.RESPONSE_TYPE, "code");
  850. OAuthFlows oauth = Flows.oauth(session, realm, request, uriInfo, authManager, tokenManager);
  851. if (!checkSsl()) {
  852. return oauth.forwardToSecurityFailure("HTTPS required");
  853. }
  854. String code = formData.getFirst(OAuth2Constants.CODE);
  855. AccessCode accessCode = AccessCode.parse(code, session, realm);
  856. if (accessCode == null || !accessCode.isValid(ClientSessionModel.Action.OAUTH_GRANT)) {
  857. audit.error(Errors.INVALID_CODE);
  858. return oauth.forwardToSecurityFailure("Invalid access code.");
  859. }
  860. audit.detail(Details.CODE_ID, accessCode.getCodeId());
  861. String redirect = accessCode.getRedirectUri();
  862. String state = accessCode.getState();
  863. audit.client(accessCode.getClient())
  864. .user(accessCode.getUser())
  865. .detail(Details.RESPONSE_TYPE, "code")
  866. .detail(Details.REDIRECT_URI, redirect);
  867. UserSessionModel userSession = session.sessions().getUserSession(realm, accessCode.getSessionState());
  868. if (userSession != null) {
  869. audit.detail(Details.AUTH_METHOD, userSession.getAuthMethod());
  870. audit.detail(Details.USERNAME, userSession.getLoginUsername());
  871. if (userSession.isRememberMe()) {
  872. audit.detail(Details.REMEMBER_ME, "true");
  873. }
  874. }
  875. if (!AuthenticationManager.isSessionValid(realm, userSession)) {
  876. AuthenticationManager.logout(session, realm, userSession, uriInfo);
  877. audit.error(Errors.INVALID_CODE);
  878. return oauth.forwardToSecurityFailure("Session not active");
  879. }
  880. audit.session(userSession);
  881. if (formData.containsKey("cancel")) {
  882. audit.error(Errors.REJECTED_BY_USER);
  883. return redirectAccessDenied(redirect, state);
  884. }
  885. audit.success();
  886. accessCode.setAction(ClientSessionModel.Action.CODE_TO_TOKEN);
  887. return oauth.redirectAccessCode(accessCode, userSession, state, redirect);
  888. }
  889. @Path("oauth/oob")
  890. @GET
  891. public Response installedAppUrnCallback(final @QueryParam("code") String code, final @QueryParam("error") String error, final @QueryParam("error_description") String errorDescription) {
  892. LoginFormsProvider forms = Flows.forms(session, realm, uriInfo);
  893. if (code != null) {
  894. return forms.setAccessCode(code).createCode();
  895. } else {
  896. return forms.setError(error).createCode();
  897. }
  898. }
  899. protected Response redirectAccessDenied(String redirect, String state) {
  900. UriBuilder redirectUri = UriBuilder.fromUri(redirect).queryParam(OAuth2Constants.ERROR, "access_denied");
  901. if (state != null)
  902. redirectUri.queryParam(OAuth2Constants.STATE, state);
  903. Response.ResponseBuilder location = Response.status(302).location(redirectUri.build());
  904. return location.build();
  905. }
  906. public static boolean matchesRedirects(Set<String> validRedirects, String redirect) {
  907. for (String validRedirect : validRedirects) {
  908. if (validRedirect.endsWith("*")) {
  909. // strip off *
  910. int length = validRedirect.length() - 1;
  911. validRedirect = validRedirect.substring(0, length);
  912. if (redirect.startsWith(validRedirect)) return true;
  913. // strip off trailing '/'
  914. if (length - 1 > 0 && validRedirect.charAt(length - 1) == '/') length--;
  915. validRedirect = validRedirect.substring(0, length);
  916. if (validRedirect.equals(redirect)) return true;
  917. } else if (validRedirect.equals(redirect)) return true;
  918. }
  919. return false;
  920. }
  921. public static String verifyRedirectUri(UriInfo uriInfo, String redirectUri, RealmModel realm, ClientModel client) {
  922. Set<String> validRedirects = client.getRedirectUris();
  923. if (redirectUri == null) {
  924. if (validRedirects.size() != 1) return null;
  925. String validRedirect = validRedirects.iterator().next();
  926. int idx = validRedirect.indexOf("/*");
  927. if (idx > -1) {
  928. validRedirect = validRedirect.substring(0, idx);
  929. }
  930. redirectUri = validRedirect;
  931. } else if (validRedirects.isEmpty()) {
  932. logger.error("Redirect URI is required for client: " + client.getClientId());
  933. redirectUri = null;
  934. } else {
  935. String r = redirectUri.indexOf('?') != -1 ? redirectUri.substring(0, redirectUri.indexOf('?')) : redirectUri;
  936. Set<String> resolveValidRedirects = resolveValidRedirects(uriInfo, validRedirects);
  937. boolean valid = matchesRedirects(resolveValidRedirects, r);
  938. if (!valid && r.startsWith(Constants.INSTALLED_APP_URL) && r.indexOf(':', Constants.INSTALLED_APP_URL.length()) >= 0) {
  939. int i = r.indexOf(':', Constants.INSTALLED_APP_URL.length());
  940. StringBuilder sb = new StringBuilder();
  941. sb.append(r.substring(0, i));
  942. i = r.indexOf('/', i);
  943. if (i >= 0) {
  944. sb.append(r.substring(i));
  945. }
  946. r = sb.toString();
  947. valid = matchesRedirects(resolveValidRedirects, r);
  948. }
  949. redirectUri = valid ? redirectUri : null;
  950. }
  951. if (Constants.INSTALLED_APP_URN.equals(redirectUri)) {
  952. return Urls.realmInstalledAppUrnCallback(uriInfo.getBaseUri(), realm.getName()).toString();
  953. } else {
  954. return redirectUri;
  955. }
  956. }
  957. public static Set<String> resolveValidRedirects(UriInfo uriInfo, Set<String> validRedirects) {
  958. // If the valid redirect URI is relative (no scheme, host, port) then use the request's scheme, host, and port
  959. Set<String> resolveValidRedirects = new HashSet<String>();
  960. for (String validRedirect : validRedirects) {
  961. if (validRedirect.startsWith("/")) {
  962. URI baseUri = uriInfo.getBaseUri();
  963. String uri = baseUri.