Search in sources :

Example 1 with OAuthProviderType

use of org.apache.oltu.oauth2.common.OAuthProviderType in project intermine by intermine.

the class Authenticator method execute.

/**
 * Method called for login in
 *
 * @param mapping The ActionMapping used to select this instance
 * @param form The optional ActionForm bean for this request (if any)
 * @param request The HTTP request we are processing
 * @param response The HTTP response we are creating
 * @return an ActionForward object defining where control goes next
 * @exception Exception if the application business logic throws an exception
 */
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    OAuthClientRequest authRequest;
    OAuthProviderType provider;
    Properties webProperties = InterMineContext.getWebProperties();
    // Suitable values are: GOOGLE, GITHUB, FACEBOOK, etc.
    String providerName = request.getParameter("provider");
    String redirectUri = getRedirectUri(webProperties, providerName);
    String realm = webProperties.getProperty("webapp.baseurl");
    String state = UUID.randomUUID().toString();
    request.getSession().setAttribute("oauth2.state", state);
    String authorisationUrl = webProperties.getProperty("oauth2." + providerName + ".url.auth");
    if (authorisationUrl == null) {
        try {
            provider = OAuthProviderType.valueOf(providerName);
            authorisationUrl = provider.getAuthzEndpoint();
        } catch (IllegalArgumentException e) {
            ActionErrors errors = new ActionErrors();
            errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("oauth2.error.unknown-provider"));
            saveErrors(request, errors);
            return mapping.findForward("login");
        }
    }
    try {
        authRequest = OAuthClientRequest.authorizationLocation(authorisationUrl).setClientId(webProperties.getProperty("oauth2." + providerName + ".client-id")).setRedirectURI(redirectUri).setScope(webProperties.getProperty("oauth2." + providerName + ".scopes")).setState(state).setParameter("response_type", "code").setParameter("openid.realm", // link open-id 2.0 accounts [1]
        realm).buildQueryMessage();
        String goHere = authRequest.getLocationUri();
        // various providers require the response_type parameter.
        LOG.info("[OAuth2]: Redirecting to " + goHere);
        response.sendRedirect(goHere);
        return null;
    } catch (OAuthSystemException e) {
        ActionErrors errors = new ActionErrors();
        errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("oauth2.error.system-exception", e));
        saveErrors(request, errors);
        return mapping.findForward("login");
    }
// [1]: see https://developers.google.com/identity/protocols/OpenID2Migration
}
Also used : OAuthProviderType(org.apache.oltu.oauth2.common.OAuthProviderType) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) ActionMessage(org.apache.struts.action.ActionMessage) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) Properties(java.util.Properties) ActionErrors(org.apache.struts.action.ActionErrors)

Example 2 with OAuthProviderType

use of org.apache.oltu.oauth2.common.OAuthProviderType in project intermine by intermine.

the class AuthenticatorService method execute.

@Override
protected void execute() throws Exception {
    String providerName = getRequiredParameter("provider");
    String realm = webProperties.getProperty("webapp.baseurl");
    String authorisationUrl = webProperties.getProperty("oauth2." + providerName + ".url.auth");
    if (authorisationUrl == null) {
        try {
            OAuthProviderType providerType = OAuthProviderType.valueOf(providerName);
            authorisationUrl = providerType.getAuthzEndpoint();
        } catch (IllegalArgumentException ex) {
            throw new BadRequestException("Provider name " + providerName + " unknown");
        }
    }
    OAuthClientRequest authRequest = OAuthClientRequest.authorizationLocation(authorisationUrl).setClientId(webProperties.getProperty("oauth2." + providerName + ".client-id")).setScope(webProperties.getProperty("oauth2." + providerName + ".scopes")).setParameter("response_type", "code").setParameter("openid.realm", // link open-id 2.0 accounts [1]
    realm).buildQueryMessage();
    String link = authRequest.getLocationUri();
    addResultEntry("link", link, false);
}
Also used : OAuthProviderType(org.apache.oltu.oauth2.common.OAuthProviderType) BadRequestException(org.intermine.webservice.server.exceptions.BadRequestException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Aggregations

OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)2 OAuthProviderType (org.apache.oltu.oauth2.common.OAuthProviderType)2 Properties (java.util.Properties)1 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)1 ActionErrors (org.apache.struts.action.ActionErrors)1 ActionMessage (org.apache.struts.action.ActionMessage)1 BadRequestException (org.intermine.webservice.server.exceptions.BadRequestException)1