Search in sources :

Example 21 with OAuth

use of org.apache.oltu.oauth2.common.OAuth in project BIMserver by opensourceBIM.

the class OAuthServiceImpl method registerRemoteApplication.

public SOAuthServer registerRemoteApplication(String redirectUrl, String name, String description) throws UserException {
    try {
        OAuthClientRequest request = OAuthClientRegistrationRequest.location(getBimServer().getServerSettingsCache().getServerSettings().getSiteAddress() + "/oauth/register/", OAuthRegistration.Type.PUSH).setName(name).setUrl(redirectUrl).setDescription(description).setRedirectURL(redirectUrl).buildJSONMessage();
        OAuthRegistrationClient oauthclient = new OAuthRegistrationClient(new org.bimserver.webservices.impl.URLConnectionClient());
        OAuthClientRegistrationResponse response = oauthclient.clientInfo(request);
        SOAuthServer server = new SOAuthServer();
        server.setClientId(response.getClientId());
        server.setClientSecret(response.getClientSecret());
        return server;
    } catch (Exception e) {
        throw new UserException(e);
    }
}
Also used : OAuthRegistrationClient(org.apache.oltu.oauth2.ext.dynamicreg.client.OAuthRegistrationClient) OAuthClientRegistrationResponse(org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse) SOAuthServer(org.bimserver.interfaces.objects.SOAuthServer) UserException(org.bimserver.shared.exceptions.UserException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Example 22 with OAuth

use of org.apache.oltu.oauth2.common.OAuth in project irida by phac-nml.

the class OltuAuthorizationController method authenticate.

/**
 * Begin authentication procedure by redirecting to remote authorization
 * location
 *
 * @param remoteAPI
 *            The API we need to authenticate with
 * @param redirect
 *            The location to redirect back to after authentication is
 *            complete
 * @return A ModelAndView beginning the authentication procedure
 * @throws OAuthSystemException
 */
/**
 * Begin authentication procedure by redirecting to remote authorization
 * location
 *
 * @param serviceURI
 *            The base URI of the rest api service
 * @param clientID
 *            The Client ID to connect with
 * @param clientSecret
 *            The client secret to connect with
 * @param redirect
 *            Page to redirect to after auth is complete
 * @return ModelAndView redirecting to the authorization location
 * @throws OAuthSystemException
 */
public ModelAndView authenticate(String serviceURI, String clientID, String clientSecret, String redirect) throws OAuthSystemException {
    // save the client credentials and information
    this.clientId = clientID;
    this.clientSecret = clientSecret;
    this.serviceURI = serviceURI;
    // build the authorization path
    URI serviceAuthLocation = UriBuilder.fromUri(serviceURI).path("oauth").path("authorize").build();
    logger.debug("redirect: " + redirect);
    // build a redirect URI to redirect to after auth flow is completed
    String tokenRedirect = buildRedirectURI(redirect);
    // build the redirect query to request an authorization code from the
    // remote API
    OAuthClientRequest request = OAuthClientRequest.authorizationLocation(serviceAuthLocation.toString()).setClientId(clientID).setRedirectURI(tokenRedirect).setResponseType(ResponseType.CODE.toString()).setScope("read").buildQueryMessage();
    String locURI = request.getLocationUri();
    logger.debug("authorization request location:" + locURI);
    // create the redirection
    ModelAndView modelAndView = new ModelAndView(new RedirectView(locURI));
    return modelAndView;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) URI(java.net.URI)

Example 23 with OAuth

use of org.apache.oltu.oauth2.common.OAuth in project irida by phac-nml.

the class OltuAuthorizationController method getToken.

/**
 * Receive the OAuth2 authorization code and request an OAuth2 token
 *
 * @param request
 *            The incoming request
 * @param response
 *            The response to redirect
 * @param apiId
 *            the Long ID of the API we're requesting from
 * @param redirect
 *            The URL location to redirect to after completion
 * @return A ModelAndView redirecting back to the resource that was
 *         requested
 * @throws IOException
 * @throws OAuthSystemException
 * @throws OAuthProblemException
 * @throws URISyntaxException
 */
@RequestMapping("/token")
public ModelAndView getToken(HttpServletRequest request, HttpServletResponse response, @RequestParam("redirect") String redirect) throws IOException, OAuthSystemException, OAuthProblemException, URISyntaxException {
    // Get the OAuth2 auth code
    OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
    String code = oar.getCode();
    logger.debug("got code " + code);
    // Read the RemoteAPI from the RemoteAPIService and get the base URI
    // Build the token location for this service
    URI serviceTokenLocation = UriBuilder.fromUri(serviceURI).path("oauth").path("token").build();
    logger.debug("token loc " + serviceTokenLocation);
    // Build the redirect URI to request a token from
    String tokenRedirect = buildRedirectURI(redirect);
    // Create the token request form the given auth code
    OAuthClientRequest tokenRequest = OAuthClientRequest.tokenLocation(serviceTokenLocation.toString()).setClientId(clientId).setClientSecret(clientSecret).setRedirectURI(tokenRedirect).setCode(code).setGrantType(GrantType.AUTHORIZATION_CODE).buildBodyMessage();
    // execute the request
    OAuthClient client = new OAuthClient(new URLConnectionClient());
    // read the response for the access token
    OAuthJSONAccessTokenResponse accessTokenResponse = client.accessToken(tokenRequest, OAuthJSONAccessTokenResponse.class);
    String accessToken = accessTokenResponse.getAccessToken();
    // check the token expiry
    Long expiresIn = accessTokenResponse.getExpiresIn();
    logger.debug("Token expires in " + expiresIn);
    // adding the token to the response page. This is just a demo to show
    // how to get an oauth token. NEVER DO THIS!!!
    redirect = redirect + "?token=" + accessToken;
    // redirect the response back to the requested resource
    return new ModelAndView(new RedirectView(redirect));
}
Also used : URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) OAuthJSONAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) OAuthAuthzResponse(org.apache.oltu.oauth2.client.response.OAuthAuthzResponse) URI(java.net.URI) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 24 with OAuth

use of org.apache.oltu.oauth2.common.OAuth in project irida by phac-nml.

the class GalaxyRedirectionEndpointController method passAuthCode.

/**
 * Receive the OAuth2 authorization code from IRIDA and pass it on to the client-side code
 * @param model
 *            the model to write to
 * @param request
 *            the incoming request
 * @param session
 *            the user's session
 * @return a template that will pass on the authorization code
 * @throws OAuthProblemException if a valid OAuth authorization response cannot be created
 * @throws IllegalStateException if the callback URL is removed from an invalid session
 */
@RequestMapping("galaxy/auth_code")
public String passAuthCode(Model model, HttpServletRequest request, HttpSession session) throws OAuthProblemException, IllegalStateException {
    logger.debug("Parsing auth code from HttpServletRequest");
    // Get the OAuth2 authorization code
    OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
    String code = oar.getCode();
    model.addAttribute("auth_code", code);
    session.removeAttribute("galaxyExportToolCallbackURL");
    return "templates/galaxy_auth_code.tmpl";
}
Also used : OAuthAuthzResponse(org.apache.oltu.oauth2.client.response.OAuthAuthzResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with OAuth

use of org.apache.oltu.oauth2.common.OAuth in project irida by phac-nml.

the class OltuAuthorizationController method authenticate.

/**
 * Begin authentication procedure by redirecting to remote authorization
 * location
 *
 * @param remoteAPI
 *            The API we need to authenticate with
 * @param redirect
 *            The location to redirect back to after authentication is
 *            complete
 * @return A ModelAndView beginning the authentication procedure
 * @throws OAuthSystemException
 *             if we can't read from the authorization server.
 */
public String authenticate(RemoteAPI remoteAPI, String redirect) throws OAuthSystemException {
    // get the URI for the remote service we'll be requesting from
    String serviceURI = remoteAPI.getServiceURI();
    // build the authorization path
    URI serviceAuthLocation = UriBuilder.fromUri(serviceURI).path("oauth").path("authorize").build();
    logger.debug("Authenticating for service: " + remoteAPI);
    logger.debug("Redirect after authentication: " + redirect);
    // build a redirect URI to redirect to after auth flow is completed
    String tokenRedirect = buildRedirectURI(remoteAPI.getId(), redirect);
    // build the redirect query to request an authorization code from the
    // remote API
    OAuthClientRequest request = OAuthClientRequest.authorizationLocation(serviceAuthLocation.toString()).setClientId(remoteAPI.getClientId()).setRedirectURI(tokenRedirect).setResponseType(ResponseType.CODE.toString()).setScope("read").buildQueryMessage();
    String locURI = request.getLocationUri();
    logger.trace("Authorization request location: " + locURI);
    return "redirect:" + locURI;
}
Also used : OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) URI(java.net.URI)

Aggregations

OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)22 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)17 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)14 OAuthResponse (org.apache.oltu.oauth2.common.message.OAuthResponse)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 URI (java.net.URI)9 HashMap (java.util.HashMap)7 OAuthIssuerImpl (org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl)6 OAuthASResponse (org.apache.oltu.oauth2.as.response.OAuthASResponse)6 ResponseEntity (org.springframework.http.ResponseEntity)6 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)6 MD5Generator (org.apache.oltu.oauth2.as.issuer.MD5Generator)5 OAuthAuthzRequest (org.apache.oltu.oauth2.as.request.OAuthAuthzRequest)5 OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 OAuthAuthzResponse (org.apache.oltu.oauth2.client.response.OAuthAuthzResponse)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ServletException (javax.servlet.ServletException)3