Java Code Examples for org.wso2.carbon.identity.oauth2.util.OAuth2Util#getTenantId()
The following examples show how to use
org.wso2.carbon.identity.oauth2.util.OAuth2Util#getTenantId() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TokenMgtDAO.java From carbon-identity with Apache License 2.0 | 5 votes |
public void persistAuthorizationCode(String authzCode, String consumerKey, String callbackUrl, AuthzCodeDO authzCodeDO) throws IdentityOAuth2Exception { if (!enablePersist) { return; } Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement prepStmt = null; try { prepStmt = connection.prepareStatement(SQLQueries.STORE_AUTHORIZATION_CODE); prepStmt.setString(1, authzCodeDO.getAuthzCodeId()); prepStmt.setString(2, persistenceProcessor.getProcessedAuthzCode(authzCode)); prepStmt.setString(3, callbackUrl); prepStmt.setString(4, OAuth2Util.buildScopeString(authzCodeDO.getScope())); prepStmt.setString(5, authzCodeDO.getAuthorizedUser().getUserName()); prepStmt.setString(6, authzCodeDO.getAuthorizedUser().getUserStoreDomain()); int tenantId = OAuth2Util.getTenantId(authzCodeDO.getAuthorizedUser().getTenantDomain()); prepStmt.setInt(7, tenantId); prepStmt.setTimestamp(8, authzCodeDO.getIssuedTime(), Calendar.getInstance(TimeZone.getTimeZone(UTC))); prepStmt.setLong(9, authzCodeDO.getValidityPeriod()); prepStmt.setString(10, authzCodeDO.getAuthorizedUser().getAuthenticatedSubjectIdentifier()); prepStmt.setString(11, persistenceProcessor.getProcessedClientId(consumerKey)); prepStmt.execute(); connection.commit(); } catch (SQLException e) { throw new IdentityOAuth2Exception("Error when storing the authorization code for consumer key : " + consumerKey, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, prepStmt); } }
Example 2
Source File: TokenMgtDAO.java From carbon-identity with Apache License 2.0 | 5 votes |
/** * * @param authenticatedUser * @return * @throws IdentityOAuth2Exception */ public Set<String> getAuthorizationCodesForUser(AuthenticatedUser authenticatedUser) throws IdentityOAuth2Exception { Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement ps = null; ResultSet rs = null; Set<String> authorizationCodes = new HashSet<>(); boolean isUsernameCaseSensitive = IdentityUtil.isUserStoreInUsernameCaseSensitive(authenticatedUser.toString()); try { String sqlQuery = SQLQueries.GET_AUTHORIZATION_CODES_BY_AUTHZUSER; if (!isUsernameCaseSensitive) { sqlQuery = sqlQuery.replace(AUTHZ_USER, LOWER_AUTHZ_USER); } ps = connection.prepareStatement(sqlQuery); if (isUsernameCaseSensitive) { ps.setString(1, authenticatedUser.getUserName()); } else { ps.setString(1, authenticatedUser.getUserName().toLowerCase()); } ps.setString(2,Integer.toString(OAuth2Util.getTenantId(authenticatedUser.getTenantDomain()))); ps.setString(3, authenticatedUser.getUserStoreDomain()); rs = ps.executeQuery(); while (rs.next()){ authorizationCodes.add(rs.getString(1)); } connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new IdentityOAuth2Exception("Error occurred while revoking Access Token with user Name : " + authenticatedUser.getUserName() + " tenant ID : " + OAuth2Util.getTenantId(authenticatedUser .getTenantDomain()), e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, ps); } return authorizationCodes; }
Example 3
Source File: JWTTokenGenerator.java From carbon-identity with Apache License 2.0 | 5 votes |
private Key getPrivateKey(String tenantDomain, int tenantId) throws IdentityOAuth2Exception { if (tenantDomain == null) { tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME; } if (tenantId == 0) { tenantId = OAuth2Util.getTenantId(tenantDomain); } Key privateKey = null; if (!(privateKeys.containsKey(tenantId))) { // get tenant's key store manager KeyStoreManager tenantKSM = KeyStoreManager.getInstance(tenantId); if (!tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) { // derive key store name String ksName = tenantDomain.trim().replace(".", "-"); String jksName = ksName + ".jks"; // obtain private key privateKey = tenantKSM.getPrivateKey(jksName, tenantDomain); } else { try { privateKey = tenantKSM.getDefaultPrivateKey(); } catch (Exception e) { log.error("Error while obtaining private key for super tenant", e); } } if (privateKey != null) { privateKeys.put(tenantId, privateKey); } } else { privateKey = privateKeys.get(tenantId); } return privateKey; }
Example 4
Source File: JWTTokenGenerator.java From carbon-identity with Apache License 2.0 | 5 votes |
private Certificate getCertificate(String tenantDomain, int tenantId) throws Exception { if (tenantDomain == null) { tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME; } if (tenantId == 0) { tenantId = OAuth2Util.getTenantId(tenantDomain); } Certificate publicCert = null; if (!(publicCerts.containsKey(tenantId))) { // get tenant's key store manager KeyStoreManager tenantKSM = KeyStoreManager.getInstance(tenantId); KeyStore keyStore = null; if (!tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) { // derive key store name String ksName = tenantDomain.trim().replace(".", "-"); String jksName = ksName + ".jks"; keyStore = tenantKSM.getKeyStore(jksName); publicCert = keyStore.getCertificate(tenantDomain); } else { publicCert = tenantKSM.getDefaultPrimaryCertificate(); } if (publicCert != null) { publicCerts.put(tenantId, publicCert); } } else { publicCert = publicCerts.get(tenantId); } return publicCert; }
Example 5
Source File: TokenMgtDAO.java From carbon-identity with Apache License 2.0 | 4 votes |
/** * @param authenticatedUser * @return * @throws IdentityOAuth2Exception */ public Set<String> getAccessTokensForUser(AuthenticatedUser authenticatedUser) throws IdentityOAuth2Exception { String accessTokenStoreTable = OAuthConstants.ACCESS_TOKEN_STORE_TABLE; Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement ps = null; ResultSet rs = null; Set<String> accessTokens = new HashSet<>(); boolean isUsernameCaseSensitive = IdentityUtil.isUserStoreInUsernameCaseSensitive(authenticatedUser.toString()); try { if (OAuth2Util.checkAccessTokenPartitioningEnabled() && OAuth2Util.checkUserNameAssertionEnabled()) { accessTokenStoreTable = OAuth2Util.getAccessTokenStoreTableFromUserId(authenticatedUser.toString()); } String sqlQuery = SQLQueries.GET_ACCESS_TOKEN_BY_AUTHZUSER.replace( IDN_OAUTH2_ACCESS_TOKEN, accessTokenStoreTable); if (!isUsernameCaseSensitive){ sqlQuery = sqlQuery.replace(AUTHZ_USER, LOWER_AUTHZ_USER); } ps = connection.prepareStatement(sqlQuery); if (isUsernameCaseSensitive) { ps.setString(1, authenticatedUser.getUserName()); } else { ps.setString(1, authenticatedUser.getUserName().toLowerCase()); } ps.setString(2, Integer.toString(OAuth2Util.getTenantId(authenticatedUser.getTenantDomain()))); ps.setString(3, OAuthConstants.TokenStates.TOKEN_STATE_ACTIVE); ps.setString(4, authenticatedUser.getUserStoreDomain()); rs = ps.executeQuery(); while (rs.next()){ accessTokens.add(rs.getString(1)); } connection.commit(); } catch (SQLException e) { IdentityDatabaseUtil.rollBack(connection); throw new IdentityOAuth2Exception("Error occurred while revoking Access Token with user Name : " + authenticatedUser.getUserName() + " tenant ID : " + OAuth2Util.getTenantId(authenticatedUser .getTenantDomain()), e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, ps); } return accessTokens; }
Example 6
Source File: TokenMgtDAO.java From carbon-identity with Apache License 2.0 | 4 votes |
/** * This method is to list the application authorized by OAuth resource owners * * @param authzUser username of the resource owner * @return set of distinct client IDs authorized by user until now * @throws IdentityOAuth2Exception if failed to update the access token */ public Set<String> getAllTimeAuthorizedClientIds(AuthenticatedUser authzUser) throws IdentityOAuth2Exception { String accessTokenStoreTable = OAuthConstants.ACCESS_TOKEN_STORE_TABLE; PreparedStatement ps = null; Connection connection = IdentityDatabaseUtil.getDBConnection();; ResultSet rs = null; Set<String> distinctConsumerKeys = new HashSet<>(); boolean isUsernameCaseSensitive = IdentityUtil.isUserStoreInUsernameCaseSensitive(authzUser.toString()); String tenantDomain = authzUser.getTenantDomain(); String tenantAwareUsernameWithNoUserDomain = authzUser.getUserName(); String userDomain = authzUser.getUserStoreDomain(); if ((userDomain != null)){ userDomain.toUpperCase(); } try { int tenantId = OAuth2Util.getTenantId(tenantDomain); if (OAuth2Util.checkAccessTokenPartitioningEnabled() && OAuth2Util.checkUserNameAssertionEnabled()) { accessTokenStoreTable = OAuth2Util.getAccessTokenStoreTableFromUserId(authzUser.toString()); } String sqlQuery = SQLQueries.GET_DISTINCT_APPS_AUTHORIZED_BY_USER_ALL_TIME.replace( IDN_OAUTH2_ACCESS_TOKEN, accessTokenStoreTable); if (!isUsernameCaseSensitive) { sqlQuery = sqlQuery.replace(AUTHZ_USER, LOWER_AUTHZ_USER); } ps = connection.prepareStatement(sqlQuery); if (isUsernameCaseSensitive) { ps.setString(1, tenantAwareUsernameWithNoUserDomain); } else { ps.setString(1, tenantAwareUsernameWithNoUserDomain.toLowerCase()); } ps.setInt(2, tenantId); ps.setString(3, userDomain); rs = ps.executeQuery(); while (rs.next()) { String consumerKey = persistenceProcessor.getPreprocessedClientId(rs.getString(1)); distinctConsumerKeys.add(consumerKey); } } catch (SQLException e) { throw new IdentityOAuth2Exception( "Error occurred while retrieving all distinct Client IDs authorized by " + "User ID : " + authzUser + " until now", e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, rs, ps); } return distinctConsumerKeys; }