private boolean isUserAuthorized(String clientId) throws DCRMServerException { OAuthConsumerAppDTO[] oAuthConsumerAppDTOS; try { // Get applications owned by the user oAuthConsumerAppDTOS = oAuthAdminService.getAllOAuthApplicationData(); for (OAuthConsumerAppDTO appDTO : oAuthConsumerAppDTOS) { if (clientId.equals(appDTO.getOauthConsumerKey())) { return true; } } } catch (IdentityOAuthAdminException e) { throw DCRMUtils.generateServerException( DCRMConstants.ErrorMessages.FAILED_TO_GET_APPLICATION_BY_ID, clientId, e); } return false; }
private void updateServiceProviderWithOAuthAppDetails(ServiceProvider serviceProvider, OAuthConsumerAppDTO createdApp, String applicationOwner, String tenantDomain) throws DCRMException { // Update created service provider, InboundAuthenticationConfig with OAuth application info. InboundAuthenticationConfig inboundAuthenticationConfig = new InboundAuthenticationConfig(); List<InboundAuthenticationRequestConfig> inboundAuthenticationRequestConfigs = new ArrayList<>(); InboundAuthenticationRequestConfig inboundAuthenticationRequestConfig = new InboundAuthenticationRequestConfig(); inboundAuthenticationRequestConfig.setInboundAuthKey(createdApp.getOauthConsumerKey()); inboundAuthenticationRequestConfig.setInboundAuthType(AUTH_TYPE_OAUTH_2); inboundAuthenticationRequestConfigs.add(inboundAuthenticationRequestConfig); inboundAuthenticationConfig.setInboundAuthenticationRequestConfigs(inboundAuthenticationRequestConfigs .toArray(new InboundAuthenticationRequestConfig[inboundAuthenticationRequestConfigs .size()])); serviceProvider.setInboundAuthenticationConfig(inboundAuthenticationConfig); //Set SaaS app option serviceProvider.setSaasApp(false); // Update the Service Provider app to add OAuthApp as an Inbound Authentication Config updateServiceProvider(serviceProvider, tenantDomain, applicationOwner); }
private Application buildResponse(OAuthConsumerAppDTO createdApp) { Application application = new Application(); application.setClient_name(createdApp.getApplicationName()); application.setClient_id(createdApp.getOauthConsumerKey()); application.setClient_secret(createdApp.getOauthConsumerSecret()); List<String> redirectUrisList = new ArrayList<>(); redirectUrisList.add(createdApp.getCallbackUrl()); application.setRedirect_uris(redirectUrisList); return application; }
/** * Get OAuth2/OIDC application information with client name * * @param clientName * @return Application * @throws DCRMException */ public Application getApplicationByName(String clientName) throws DCRMException { if (StringUtils.isEmpty(clientName)) { throw DCRMUtils.generateClientException( DCRMConstants.ErrorMessages.BAD_REQUEST_INSUFFICIENT_DATA, null); } String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); if (!isServiceProviderExist(clientName, tenantDomain)) { throw DCRMUtils.generateClientException( DCRMConstants.ErrorMessages.NOT_FOUND_APPLICATION_WITH_NAME, clientName); } try { OAuthConsumerAppDTO oAuthConsumerAppDTO = oAuthAdminService.getOAuthApplicationDataByAppName(clientName); if (!isUserAuthorized(oAuthConsumerAppDTO.getOauthConsumerKey())) { throw DCRMUtils.generateClientException( DCRMConstants.ErrorMessages.FORBIDDEN_UNAUTHORIZED_USER, clientName); } return buildResponse(oAuthConsumerAppDTO); } catch (IdentityOAuthAdminException e) { throw DCRMUtils.generateServerException( DCRMConstants.ErrorMessages.FAILED_TO_GET_APPLICATION, clientName, e); } }
if (StringUtils.isEmpty(consumerAppDTO.getOauthConsumerKey()) || StringUtils.isEmpty(consumerAppDTO .getOauthConsumerSecret())) { errorMessage = "OauthConsumerKey or OauthConsumerSecret is not provided for " + OAuthAppDO oauthappdo; try { oauthappdo = dao.getAppInformation(consumerAppDTO.getOauthConsumerKey()); if (oauthappdo == null) { if (log.isDebugEnabled()) { log.debug("Error while retrieving the app information using " + "provided OauthConsumerKey: " + consumerAppDTO.getOauthConsumerKey()); if (log.isDebugEnabled()) { log.debug("Invalid oauthConsumerSecret is provided for updating the OAuth" + " application with ConsumerKey: " + consumerAppDTO.getOauthConsumerKey()); String consumerKey = consumerAppDTO.getOauthConsumerKey();
InboundAuthenticationRequestConfig(); inboundAuthenticationRequestConfig.setInboundAuthKey(createdApp.getOauthConsumerKey()); inboundAuthenticationRequestConfig.setInboundAuthType("oauth2"); if (createdApp.getOauthConsumerSecret() != null && !createdApp. oAuthApplicationInfo.setClientId(createdApp.getOauthConsumerKey()); oAuthApplicationInfo.setCallBackURL(createdApp.getCallbackUrl()); oAuthApplicationInfo.setClientSecret(createdApp.getOauthConsumerSecret());
} catch (DCRMException ex) { deleteApplication(createdApp.getOauthConsumerKey()); throw ex;
appDTO.getOauthConsumerKey(), user, userStoreDomain, true); } catch (IdentityOAuth2Exception e) { String errorMsg = "Error occurred while retrieving access tokens issued for " + "Client ID : " + appDTO.getOauthConsumerKey() + ", User ID : " + userName; log.error(errorMsg, e); throw new IdentityOAuthAdminException(errorMsg, e); appDTO.getOauthConsumerKey(), user, userStoreDomain, OAuth2Util.buildScopeString(accessTokenDO.getScope()), true); } catch (IdentityOAuth2Exception e) { String errorMsg = "Error occurred while retrieving latest " + "access token issued for Client ID : " + appDTO.getOauthConsumerKey() + ", User ID : " + userName + " and Scope : " + OAuth2Util.buildScopeString(accessTokenDO.getScope()); log.error(errorMsg, e);
/** * Convert {@link org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO} to an * {@link org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo} * * @param createdApp Response from OAuthAdminService * @return Converted {@link org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo} */ private OAuthApplicationInfo createOAuthAppInfoFromDTO(OAuthConsumerAppDTO createdApp) { OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo(); oAuthApplicationInfo.setClientId(createdApp.getOauthConsumerKey()); oAuthApplicationInfo.setCallBackURL(createdApp.getCallbackUrl()); oAuthApplicationInfo.setClientSecret(createdApp.getOauthConsumerSecret()); oAuthApplicationInfo.addParameter(ApplicationConstants. OAUTH_REDIRECT_URIS, createdApp.getCallbackUrl()); oAuthApplicationInfo.addParameter(ApplicationConstants. OAUTH_CLIENT_NAME, createdApp.getApplicationName()); oAuthApplicationInfo.addParameter(ApplicationConstants. OAUTH_CLIENT_GRANT, createdApp.getGrantTypes()); return oAuthApplicationInfo; } }
if (application.getOauthConsumerKey() == null) { app.setOauthConsumerKey(OAuthUtil.getRandomNumber()); app.setOauthConsumerSecret(OAuthUtil.getRandomNumber()); } else { app.setOauthConsumerKey(application.getOauthConsumerKey()); app.setOauthConsumerSecret(application.getOauthConsumerSecret());
user.setUserStoreDomain(IdentityUtil.extractDomainFromName(userName)); oauthappdo.setUser(user); oauthappdo.setOauthConsumerKey(consumerAppDTO.getOauthConsumerKey()); oauthappdo.setOauthConsumerSecret(consumerAppDTO.getOauthConsumerSecret()); oauthappdo.setCallbackUrl(consumerAppDTO.getCallbackUrl());
if (StringUtils.isEmpty(application.getOauthConsumerKey())) { app.setOauthConsumerKey(OAuthUtil.getRandomNumber()); app.setOauthConsumerSecret(OAuthUtil.getRandomNumber()); } else { app.setOauthConsumerKey(application.getOauthConsumerKey()); if (StringUtils.isEmpty(application.getOauthConsumerSecret())) { app.setOauthConsumerSecret(OAuthUtil.getRandomNumber());
inboundAuthenticationRequestConfig.setInboundAuthKey(createdApp.getOauthConsumerKey()); inboundAuthenticationRequestConfig.setInboundAuthType(AUTH_TYPE_OAUTH_2); String oauthConsumerSecret = createdApp.getOauthConsumerSecret(); registrationResponseProfile.setClientId(createdApp.getOauthConsumerKey()); registrationResponseProfile.getRedirectUrls().add(createdApp.getCallbackUrl()); registrationResponseProfile.setClientSecret(oauthConsumerSecret);
.getAccessTokenDAO().getAccessTokens(appDTO.getOauthConsumerKey(), user, userStoreDomain, true); } catch (IdentityOAuth2Exception e) { String errorMsg = "Error occurred while retrieving access tokens issued for " + "Client ID : " + appDTO.getOauthConsumerKey() + ", User ID : " + userName; throw handleError(errorMsg, e); .getLatestAccessToken(appDTO.getOauthConsumerKey(), user, userStoreDomain, OAuth2Util.buildScopeString(accessTokenDO.getScope()), true); } catch (IdentityOAuth2Exception e) { String errorMsg = "Error occurred while retrieving latest " + "access token issued for Client ID : " + appDTO.getOauthConsumerKey() + ", User ID : " + userName + " and Scope : " + OAuth2Util.buildScopeString(accessTokenDO.getScope()); throw handleError(errorMsg, e);
inboundAuthenticationRequestConfig.setInboundAuthKey(createdApp.getOauthConsumerKey()); inboundAuthenticationRequestConfig.setInboundAuthType(AUTH_TYPE_OAUTH_2); String oauthConsumerSecret = createdApp.getOauthConsumerSecret(); oAuthApplicationInfo.setClientId(createdApp.getOauthConsumerKey()); oAuthApplicationInfo.setCallBackURL(createdApp.getCallbackUrl()); oAuthApplicationInfo.setClientSecret(oauthConsumerSecret);