Java Code Examples for org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO#getOauthConsumerKey()

The following examples show how to use org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO#getOauthConsumerKey() . 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: OAuthInboundFunctions.java    From identity-api-server with Apache License 2.0 5 votes vote down vote up
public static InboundAuthenticationRequestConfig putOAuthInbound(ServiceProvider application,
                                                                 OpenIDConnectConfiguration oidcConfigModel) {

    // First we identify whether this is a insert or update.
    try {
        String currentClientId = InboundFunctions.getInboundAuthKey(application, StandardInboundProtocols.OAUTH2);
        if (currentClientId != null) {
            // This is an update.
            OAuthConsumerAppDTO oauthApp = ApplicationManagementServiceHolder.getInstance().getOAuthAdminService
                    ().getOAuthApplicationData(currentClientId);

            if (!StringUtils.equals(oauthApp.getOauthConsumerKey(), oidcConfigModel.getClientId())) {
                throw buildBadRequestError("Invalid ClientID provided for update.");
            }

            if (!StringUtils.equals(oauthApp.getOauthConsumerSecret(), oidcConfigModel.getClientSecret())) {
                throw buildBadRequestError("Invalid ClientSecret provided for update.");
            }

            OAuthConsumerAppDTO appToUpdate = new ApiModelToOAuthConsumerApp().apply(application
                    .getApplicationName(), oidcConfigModel);
            ApplicationManagementServiceHolder.getInstance().getOAuthAdminService().updateConsumerApplication
                    (appToUpdate);

            String updatedClientId = appToUpdate.getOauthConsumerKey();
            return createInboundAuthRequestConfig(updatedClientId);
        } else {
            return createOAuthInbound(application.getApplicationName(), oidcConfigModel);
        }

    } catch (IdentityOAuthAdminException e) {
        throw handleOAuthException(e);
    }
}
 
Example 2
Source File: OAuthAdminService.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Registers an OAuth consumer application.
 *
 * @param application <code>OAuthConsumerAppDTO</code> with application information
 * @throws Exception Error when persisting the application information to the persistence store
 */
public void registerOAuthApplicationData(OAuthConsumerAppDTO application) throws IdentityOAuthAdminException{
    String userName = CarbonContext.getThreadLocalCarbonContext().getUsername();
    if (userName != null) {
        String tenantUser = MultitenantUtils.getTenantAwareUsername(userName);
        int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
        String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();

        OAuthAppDAO dao = new OAuthAppDAO();
        OAuthAppDO app = new OAuthAppDO();
        if (application != null) {
            app.setApplicationName(application.getApplicationName());
            if ((application.getGrantTypes().contains(AUTHORIZATION_CODE) || application.getGrantTypes()
                    .contains(IMPLICIT)) && StringUtils.isEmpty(application.getCallbackUrl())) {
                throw new IdentityOAuthAdminException("Callback Url is required for Code or Implicit grant types");
            }
            app.setCallbackUrl(application.getCallbackUrl());
            if (application.getOauthConsumerKey() == null) {
                app.setOauthConsumerKey(OAuthUtil.getRandomNumber());
                app.setOauthConsumerSecret(OAuthUtil.getRandomNumber());
            } else {
                app.setOauthConsumerKey(application.getOauthConsumerKey());
                app.setOauthConsumerSecret(application.getOauthConsumerSecret());
            }
            String applicationUser = application.getUsername();
            if (applicationUser != null && applicationUser.trim().length() > 0) {
                try {
                    if (CarbonContext.getThreadLocalCarbonContext().getUserRealm().
                            getUserStoreManager().isExistingUser(application.getUsername())) {
                        tenantUser = applicationUser;
                    } else {
                        log.warn("OAuth application registrant user name " + applicationUser +
                                " does not exist in the user store. Using logged-in user name " + tenantUser +
                                " as registrant name");
                    }
                } catch (UserStoreException e) {
                    throw new IdentityOAuthAdminException("Error while retrieving the user store manager", e);
                }

            }
            AuthenticatedUser user = new AuthenticatedUser();
            user.setUserName(UserCoreUtil.removeDomainFromName(tenantUser));
            user.setTenantDomain(tenantDomain);
            user.setUserStoreDomain(IdentityUtil.extractDomainFromName(userName));
            app.setUser(user);
            if (application.getOAuthVersion() != null) {
                app.setOauthVersion(application.getOAuthVersion());
            } else {   // by default, assume OAuth 2.0, if it is not set.
                app.setOauthVersion(OAuthConstants.OAuthVersions.VERSION_2);
            }
            if (OAuthConstants.OAuthVersions.VERSION_2.equals(application.getOAuthVersion())) {
                List<String> allowedGrants = new ArrayList<>(Arrays.asList(getAllowedGrantTypes()));
                String[] requestGrants = application.getGrantTypes().split("\\s");
                for (String requestedGrant : requestGrants) {
                    if (StringUtils.isBlank(requestedGrant)){
                        continue;
                    }
                    if (!allowedGrants.contains(requestedGrant)) {
                        throw new IdentityOAuthAdminException(requestedGrant + " not allowed");
                    }
                }
                app.setGrantTypes(application.getGrantTypes());
            }
            dao.addOAuthApplication(app);
            if (OAuthServerConfiguration.getInstance().isCacheEnabled()) {
                appInfoCache.addToCache(app.getOauthConsumerKey(), app);
            }
        }
    }
}