All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.crosstreelabs.oauth.v2.grant.AbstractTokenGranter Maven / Gradle / Ivy

The newest version!
/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.crosstreelabs.oauth.v2.grant;

import com.crosstreelabs.oauth.v2.DefaultOAuth2RequestFactory;
import com.crosstreelabs.oauth.v2.TokenGranter;
import com.crosstreelabs.oauth.v2.exception.InvalidClientException;
import com.crosstreelabs.oauth.v2.model.AccessToken;
import com.crosstreelabs.oauth.v2.model.Client;
import com.crosstreelabs.oauth.v2.AccessTokenManager;
import com.crosstreelabs.oauth.v2.io.Request;
import com.crosstreelabs.oauth.v2.principal.OAuthPrincipal;
import com.crosstreelabs.oauth.v2.service.ClientService;
import java.util.Collection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractTokenGranter implements TokenGranter {
    private static final Logger LOG = LoggerFactory.getLogger(AbstractTokenGranter.class);
    
    protected final AccessTokenManager tokenServices;
    protected final ClientService clientService;
    protected final DefaultOAuth2RequestFactory requestFactory;

    protected AbstractTokenGranter(final AccessTokenManager tokenServices,
            final ClientService clientService,
            final DefaultOAuth2RequestFactory requestFactory) {
        this.clientService = clientService;
        this.tokenServices = tokenServices;
        this.requestFactory = requestFactory;
    }

    @Override
    public AccessToken grant(final Request request) {
        if (!grantType().equals(request.getGrantType())) {
            return null;
        }
        String clientId = request.getClientId();
        Client client = clientService.find(clientId);
        validateGrantType(grantType(), client);
        return getAccessToken(client, request);
    }

    protected AccessToken getAccessToken(final Client client,
            final Request request) {
        return tokenServices.createAccessToken(getOAuth2Authentication(client, request));
    }

    protected OAuthPrincipal getOAuth2Authentication(final Client client,
            final Request request) {
        Request storedOAuth2Request = requestFactory.createOAuth2Request(client, request);
        return new OAuthPrincipal(storedOAuth2Request, client, null);
    }

    protected void validateGrantType(final String grantType,
            final Client client) {
        final Collection authorizedGrantTypes = client.getAllowedGrantTypes();
        if (authorizedGrantTypes != null && !authorizedGrantTypes.isEmpty()
                && !authorizedGrantTypes.contains(grantType)) {
            throw new InvalidClientException("Unauthorized grant type: " + grantType);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy