public String getOauthApplicationState(String consumerKey) { OAuthAppDAO oAuthAppDAO = new OAuthAppDAO(); try { return oAuthAppDAO.getConsumerAppState(consumerKey); } catch (IdentityOAuthAdminException e) { log.error("Error while getting oauth app state", e); return null; } }
/** * @return * @throws IdentityOAuthAdminException */ public String getOauthApplicationState(String consumerKey) throws IdentityOAuthAdminException { OAuthAppDAO oAuthAppDAO = new OAuthAppDAO(); return oAuthAppDAO.getConsumerAppState(consumerKey); }
/** * Removes an OAuth consumer application. * * @param consumerKey Consumer Key * @throws IdentityOAuthAdminException Error when removing the consumer information from the database. */ public void removeOAuthApplicationData(String consumerKey) throws IdentityOAuthAdminException { OAuthAppDAO dao = new OAuthAppDAO(); dao.removeConsumerApplication(consumerKey); // remove client credentials from cache OAuthCache.getInstance().clearCacheEntry(new OAuthCacheKey(consumerKey)); AppInfoCache.getInstance().clearCacheEntry(consumerKey); if (log.isDebugEnabled()) { log.debug("Client credentials are removed from the cache for OAuth App with consumerKey: " + consumerKey); } }
/** * @param consumerKey * @param newState * @throws IdentityOAuthAdminException */ public void updateConsumerAppState(String consumerKey, String newState) throws IdentityOAuthAdminException { OAuthAppDAO oAuthAppDAO = new OAuthAppDAO(); try { OAuthAppDO oAuthAppDO = AppInfoCache.getInstance().getValueFromCache(consumerKey); if (oAuthAppDO == null) { oAuthAppDO = oAuthAppDAO.getAppInformation(consumerKey); } // change the state oAuthAppDO.setState(newState); Properties properties = new Properties(); properties.setProperty(OAuthConstants.OAUTH_APP_NEW_STATE, newState); properties.setProperty(OAuthConstants.ACTION_PROPERTY_KEY, OAuthConstants.ACTION_REVOKE); updateAppAndRevokeTokensAndAuthzCodes(consumerKey, properties); AppInfoCache.getInstance().addToCache(consumerKey, oAuthAppDO); if (log.isDebugEnabled()) { log.debug("App state is updated to:" + newState + " in the AppInfoCache for OAuth App with " + "consumerKey: " + consumerKey); } } catch (InvalidOAuthClientException | IdentityOAuth2Exception e) { throw handleError("Error while updating state of OAuth app with consumerKey: " + consumerKey, e); } }
/** * Get OAuth application data by the consumer key. * * @param consumerKey Consumer Key * @return <code>OAuthConsumerAppDTO</code> with application information * @throws IdentityOAuthAdminException Error when reading application information from persistence store. */ public OAuthConsumerAppDTO getOAuthApplicationData(String consumerKey) throws IdentityOAuthAdminException { OAuthConsumerAppDTO dto; OAuthAppDAO dao = new OAuthAppDAO(); try { OAuthAppDO app = dao.getAppInformation(consumerKey); if (app != null) { dto = buildConsumerAppDTO(app); if (log.isDebugEnabled()) { log.debug("Found App :" + dto.getApplicationName() + " for consumerKey: " + consumerKey); } } else { dto = new OAuthConsumerAppDTO(); } return dto; } catch (InvalidOAuthClientException | IdentityOAuth2Exception e) { throw handleError("Error while retrieving the app information using consumerKey: " + consumerKey, e); } }
/** * Removes an OAuth consumer application. * * @param consumerKey Consumer Key * @throws Exception Error when removing the consumer information from the database. */ public void removeOAuthApplicationData(String consumerKey) throws IdentityOAuthAdminException { OAuthAppDAO dao = new OAuthAppDAO(); dao.removeConsumerApplication(consumerKey); // remove client credentials from cache if (OAuthServerConfiguration.getInstance().isCacheEnabled()) { OAuthCache.getInstance().clearCacheEntry(new OAuthCacheKey(consumerKey)); appInfoCache.clearCacheEntry(consumerKey); if (log.isDebugEnabled()) { log.debug("Client credentials are removed from the cache."); } } }
/** * Get OAuth application data by the application name. * * @param appName OAuth application name * @return <code>OAuthConsumerAppDTO</code> with application information * @throws IdentityOAuthAdminException Error when reading application information from persistence store. */ public OAuthConsumerAppDTO getOAuthApplicationDataByAppName(String appName) throws IdentityOAuthAdminException { OAuthConsumerAppDTO dto; OAuthAppDAO dao = new OAuthAppDAO(); try { OAuthAppDO app = dao.getAppInformationByAppName(appName); if (app != null) { dto = buildConsumerAppDTO(app); } else { dto = new OAuthConsumerAppDTO(); } return dto; } catch (InvalidOAuthClientException | IdentityOAuth2Exception e) { throw handleError("Error while retrieving the app information by app name: " + appName, e); } }
/** * Registers an consumer secret against the logged in user. A given user can only have a single * consumer secret at a time. Calling this method again and again will update the existing * consumer secret key. * * @return An array containing the consumer key and the consumer secret correspondingly. * @throws Exception Error when persisting the data in the persistence store. */ public String[] registerOAuthConsumer() throws IdentityOAuthAdminException { String loggedInUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); if (log.isDebugEnabled()) { log.debug("Adding a consumer secret for the logged in user " + loggedInUser); } String tenantUser = MultitenantUtils.getTenantAwareUsername(loggedInUser); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); String userDomain = IdentityUtil.extractDomainFromName(loggedInUser); OAuthAppDAO dao = new OAuthAppDAO(); return dao.addOAuthConsumer(UserCoreUtil.removeDomainFromName(tenantUser), tenantId, userDomain); }
/** * Registers an consumer secret against the logged in user. A given user can only have a single * consumer secret at a time. Calling this method again and again will update the existing * consumer secret key. * * @return An array containing the consumer key and the consumer secret correspondingly. * @throws IdentityOAuthAdminException Error when persisting the data in the persistence store. */ public String[] registerOAuthConsumer() throws IdentityOAuthAdminException { String loggedInUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); if (log.isDebugEnabled()) { log.debug("Adding a consumer secret for the logged in user " + loggedInUser); } String tenantUser = MultitenantUtils.getTenantAwareUsername(loggedInUser); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); String userDomain = IdentityUtil.extractDomainFromName(loggedInUser); OAuthAppDAO dao = new OAuthAppDAO(); return dao.addOAuthConsumer(UserCoreUtil.removeDomainFromName(tenantUser), tenantId, userDomain); }
private OAuthAppDO getAppInformation(OAuth2AuthorizeReqDTO authzReqDTO) throws IdentityOAuth2Exception, InvalidOAuthClientException { OAuthAppDO oAuthAppDO = appInfoCache.getValueFromCache(authzReqDTO.getConsumerKey()); if (oAuthAppDO != null) { return oAuthAppDO; } else { oAuthAppDO = new OAuthAppDAO().getAppInformation(authzReqDTO.getConsumerKey()); appInfoCache.addToCache(authzReqDTO.getConsumerKey(), oAuthAppDO); return oAuthAppDO; } }
/** * Get Oauth application information * * @param clientId * @return Oauth app information * @throws IdentityOAuth2Exception * @throws InvalidOAuthClientException */ public static OAuthAppDO getAppInformationByClientId(String clientId) throws IdentityOAuth2Exception, InvalidOAuthClientException { OAuthAppDO oAuthAppDO = AppInfoCache.getInstance().getValueFromCache(clientId); if (oAuthAppDO != null) { return oAuthAppDO; } else { oAuthAppDO = new OAuthAppDAO().getAppInformation(clientId); AppInfoCache.getInstance().addToCache(clientId, oAuthAppDO); return oAuthAppDO; } }
/** * Get Oauth application information * * @param clientId * @return Oauth app information * @throws IdentityOAuth2Exception * @throws InvalidOAuthClientException */ public static OAuthAppDO getAppInformationByClientId(String clientId) throws IdentityOAuth2Exception, InvalidOAuthClientException { OAuthAppDO oAuthAppDO = AppInfoCache.getInstance().getValueFromCache(clientId); if (oAuthAppDO != null) { return oAuthAppDO; } else { oAuthAppDO = new OAuthAppDAO().getAppInformation(clientId); AppInfoCache.getInstance().addToCache(clientId, oAuthAppDO); return oAuthAppDO; } }
/** * Remove oauth consumer app related properties. * * @param serviceProvider Service provider * @param tenantDomain Application tenant domain * @throws IdentityApplicationManagementException */ private void removeOauthConsumerAppProperties(ServiceProvider serviceProvider, String tenantDomain) throws IdentityApplicationManagementException { try { InboundAuthenticationConfig inboundAuthenticationConfig = serviceProvider.getInboundAuthenticationConfig(); if (inboundAuthenticationConfig != null) { InboundAuthenticationRequestConfig[] inboundRequestConfigs = inboundAuthenticationConfig. getInboundAuthenticationRequestConfigs(); if (inboundRequestConfigs != null) { for (InboundAuthenticationRequestConfig inboundRequestConfig : inboundRequestConfigs) { if (StringUtils.equals(OAUTH2, inboundRequestConfig.getInboundAuthType()) || StringUtils .equals(inboundRequestConfig.getInboundAuthType(), OAUTH)) { String oauthKey = inboundRequestConfig.getInboundAuthKey(); OAuthAppDAO oAuthAppDAO = new OAuthAppDAO(); oAuthAppDAO.removeOIDCProperties(tenantDomain, oauthKey); } } } } } catch (IdentityOAuthAdminException ex) { throw new IdentityApplicationManagementException("Error occurred while removing OIDC properties " + "for application:" + serviceProvider.getApplicationName() + " in tenant domain: " + tenantDomain); } }
private OAuthAppDO getAppInformation(OAuth2AuthorizeReqDTO authzReqDTO) throws IdentityOAuth2Exception, InvalidOAuthClientException { OAuthAppDO oAuthAppDO = AppInfoCache.getInstance().getValueFromCache(authzReqDTO.getConsumerKey()); if (oAuthAppDO != null) { return oAuthAppDO; } else { oAuthAppDO = new OAuthAppDAO().getAppInformation(authzReqDTO.getConsumerKey()); AppInfoCache.getInstance().addToCache(authzReqDTO.getConsumerKey(), oAuthAppDO); return oAuthAppDO; } }
/** * Update the application name if OAuth application presents. * * @param serviceProvider Service provider * @throws IdentityApplicationManagementException */ private void updateAuthApplication(ServiceProvider serviceProvider) throws IdentityApplicationManagementException { InboundAuthenticationRequestConfig authenticationRequestConfigConfig = null; if (serviceProvider.getInboundAuthenticationConfig() != null && serviceProvider.getInboundAuthenticationConfig() .getInboundAuthenticationRequestConfigs() != null) { for (InboundAuthenticationRequestConfig authConfig : serviceProvider.getInboundAuthenticationConfig() .getInboundAuthenticationRequestConfigs()) { if (StringUtils.equals(authConfig.getInboundAuthType(), "oauth") || StringUtils.equals(authConfig.getInboundAuthType(), "oauth2")) { authenticationRequestConfigConfig = authConfig; break; } } } if (authenticationRequestConfigConfig == null) { return; } OAuthAppDAO dao = new OAuthAppDAO(); dao.updateOAuthConsumerApp(serviceProvider.getApplicationName(), authenticationRequestConfigConfig.getInboundAuthKey()); }
@Override public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws IdentityOAuth2Exception { OAuth2AccessTokenReqDTO tokenReqDTO = tokReqMsgCtx.getOauth2AccessTokenReqDTO(); String grantType = tokenReqDTO.getGrantType(); // Load application data from the cache AppInfoCache appInfoCache = AppInfoCache.getInstance(); OAuthAppDO oAuthAppDO = appInfoCache.getValueFromCache(tokenReqDTO.getClientId()); if (oAuthAppDO == null) { try { oAuthAppDO = new OAuthAppDAO().getAppInformation(tokenReqDTO.getClientId()); appInfoCache.addToCache(tokenReqDTO.getClientId(), oAuthAppDO); } catch (InvalidOAuthClientException e) { throw new IdentityOAuth2Exception(e.getMessage(), e); } } // If the application has defined a limited set of grant types, then check the grant if (oAuthAppDO.getGrantTypes() != null && !oAuthAppDO.getGrantTypes().contains(grantType)) { if (log.isDebugEnabled()) { //Do not change this log format as these logs use by external applications log.debug("Unsupported Grant Type : " + grantType + " for client id : " + tokenReqDTO.getClientId()); } throw new IdentityOAuth2UnAuthorizedClientException("The authenticated client is not authorized to use " + "this authorization grant type"); } return true; } }
/** * Returns the scope and the web application this particular token been issued to. * * @param oauthToken OAuth request token. * @return * @throws Exception */ public Parameters getScopeAndAppName(String oauthToken) throws Exception { OAuthConsumerDAO consumerDAO = new OAuthConsumerDAO(); Parameters params = consumerDAO.getRequestToken(oauthToken); OAuthAppDAO appDAO = new OAuthAppDAO(); OAuthAppDO oauthAppDO = appDAO.getAppInformation(params.getOauthConsumerKey()); Parameters resp = new Parameters(); resp.setScope(params.getScope()); resp.setAppName(oauthAppDO.getApplicationName()); return resp; }
/** * Returns the scope and the web application this particular token been issued to. * * @param oauthToken OAuth request token. * @return * @throws Exception */ public Parameters getScopeAndAppName(String oauthToken) throws Exception { OAuthConsumerDAO consumerDAO = new OAuthConsumerDAO(); Parameters params = consumerDAO.getRequestToken(oauthToken); OAuthAppDAO appDAO = new OAuthAppDAO(); OAuthAppDO oauthAppDO = appDAO.getAppInformation(params.getOauthConsumerKey()); Parameters resp = new Parameters(); resp.setScope(params.getScope()); resp.setAppName(oauthAppDO.getApplicationName()); return resp; }
/** * Get OAuth application data by the application name. * * @param appName OAuth application name * @return <code>OAuthConsumerAppDTO</code> with application information * @throws Exception Error when reading application information from persistence store. */ public OAuthConsumerAppDTO getOAuthApplicationDataByAppName(String appName) throws IdentityOAuthAdminException { OAuthConsumerAppDTO dto = new OAuthConsumerAppDTO(); OAuthAppDAO dao = new OAuthAppDAO(); try { OAuthAppDO app = dao.getAppInformationByAppName(appName); if (app != null) { dto.setApplicationName(app.getApplicationName()); dto.setCallbackUrl(app.getCallbackUrl()); dto.setOauthConsumerKey(app.getOauthConsumerKey()); dto.setOauthConsumerSecret(app.getOauthConsumerSecret()); dto.setOAuthVersion(app.getOauthVersion()); dto.setGrantTypes(app.getGrantTypes()); dto.setPkceMandatory(app.isPkceMandatory()); dto.setPkceSupportPlain(app.isPkceSupportPlain()); } return dto; } catch (InvalidOAuthClientException | IdentityOAuth2Exception e) { throw new IdentityOAuthAdminException("Error while retrieving the app information by app name", e); } }
/** * Get OAuth application data by the consumer key. * * @param consumerKey Consumer Key * @return <code>OAuthConsumerAppDTO</code> with application information * @throws Exception Error when reading application information from persistence store. */ public OAuthConsumerAppDTO getOAuthApplicationData(String consumerKey) throws IdentityOAuthAdminException { OAuthConsumerAppDTO dto = new OAuthConsumerAppDTO(); OAuthAppDAO dao = new OAuthAppDAO(); try { OAuthAppDO app = dao.getAppInformation(consumerKey); if (app != null) { dto.setApplicationName(app.getApplicationName()); dto.setCallbackUrl(app.getCallbackUrl()); dto.setOauthConsumerKey(app.getOauthConsumerKey()); dto.setOauthConsumerSecret(app.getOauthConsumerSecret()); dto.setOAuthVersion(app.getOauthVersion()); dto.setGrantTypes(app.getGrantTypes()); dto.setPkceMandatory(app.isPkceMandatory()); dto.setPkceSupportPlain(app.isPkceSupportPlain()); } return dto; } catch (InvalidOAuthClientException | IdentityOAuth2Exception e) { throw new IdentityOAuthAdminException("Error while retrieving the app information using consumer key", e); } }