Search in sources :

Example 11 with Client

use of org.apache.cxf.rs.security.oauth2.common.Client in project cxf by apache.

the class OIDCFiltersTest method makeAuthorizationCodeInvocation.

private String makeAuthorizationCodeInvocation(WebClient client) {
    // Make initial authorization request
    client.type("application/json").accept("application/json");
    Response response = client.get();
    OAuthAuthorizationData authzData = response.readEntity(OAuthAuthorizationData.class);
    // Now call "decision" to get the authorization code grant
    client.path("decision");
    client.type("application/x-www-form-urlencoded");
    Form form = new Form();
    form.param("session_authenticity_token", authzData.getAuthenticityToken());
    form.param("client_id", authzData.getClientId());
    form.param("redirect_uri", authzData.getRedirectUri());
    if (authzData.getProposedScope() != null) {
        form.param("scope", authzData.getProposedScope());
    }
    form.param("state", authzData.getState());
    form.param("oauthDecision", "allow");
    response = client.post(form);
    return response.getHeaderString("Location");
}
Also used : Response(javax.ws.rs.core.Response) Form(javax.ws.rs.core.Form) OAuthAuthorizationData(org.apache.cxf.rs.security.oauth2.common.OAuthAuthorizationData)

Example 12 with Client

use of org.apache.cxf.rs.security.oauth2.common.Client in project tesb-rt-se by Talend.

the class ThirdPartyRegistrationService method register.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/")
public ConsumerRegistration register(MultipartBody body) {
    String appName = body.getAttachmentObject("appName", String.class);
    String appURI = body.getAttachmentObject("appURI", String.class);
    String appRedirectURI = body.getAttachmentObject("appRedirectURI", String.class);
    String appDesc = body.getAttachmentObject("appDescription", String.class);
    URI logoURI = null;
    Attachment att = body.getAttachment("appLogo");
    if (att != null) {
        InputStream logoStream = att.getObject(InputStream.class);
        CachedOutputStream cos = new CachedOutputStream();
        try {
            IOUtils.copy(logoStream, cos);
            appLogos.put(appName.toLowerCase(), cos);
            UriBuilder ub = uriInfo.getAbsolutePathBuilder();
            ub.path("logo").path(appName.toLowerCase());
            ContentDisposition cd = att.getContentDisposition();
            if (cd != null && cd.getParameter("filename") != null) {
                ub.path(cd.getParameter("filename"));
            }
            logoURI = ub.build();
        } catch (IOException ex) {
        // ignore
        }
    }
    String clientId = generateClientId(appName, appURI);
    String clientSecret = generateClientSecret();
    Client newClient = new Client(clientId, clientSecret, true, appName, appURI);
    newClient.setApplicationDescription(appDesc);
    newClient.setApplicationLogoUri(logoURI.toString());
    newClient.setRedirectUris(Collections.singletonList(appRedirectURI));
    manager.registerClient(newClient);
    return new ConsumerRegistration(clientId, clientSecret);
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) InputStream(java.io.InputStream) ConsumerRegistration(oauth2.common.ConsumerRegistration) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException) UriBuilder(javax.ws.rs.core.UriBuilder) Client(org.apache.cxf.rs.security.oauth2.common.Client) URI(java.net.URI) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 13 with Client

use of org.apache.cxf.rs.security.oauth2.common.Client in project teiid by teiid.

the class OAuth20CredentialImpl method getAccessToken.

protected ClientAccessToken getAccessToken() {
    if (getAccessTokenString() != null) {
        // if we have access_token directly, use it
        return new ClientAccessToken(OAuthConstants.ACCESS_TOKEN_TYPE, getAccessTokenString());
    }
    Consumer consumer = new Consumer(getClientId(), getClientSecret());
    WebClient client = WebClient.create(getAccessTokenURI());
    RefreshTokenGrant grant = new RefreshTokenGrant(getRefreshToken());
    return OAuthClientUtils.getAccessToken(client, consumer, grant, null, "Bearer", false);
}
Also used : Consumer(org.apache.cxf.rs.security.oauth2.client.Consumer) RefreshTokenGrant(org.apache.cxf.rs.security.oauth2.grants.refresh.RefreshTokenGrant) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 14 with Client

use of org.apache.cxf.rs.security.oauth2.common.Client in project testcases by coheigea.

the class AuthorizationGrantTest method testAuthorizationCodeGrant.

@org.junit.Test
public void testAuthorizationCodeGrant() throws Exception {
    URL busFile = AuthorizationGrantTest.class.getResource("cxf-client.xml");
    String address = "https://localhost:" + PORT + "/services/";
    WebClient client = WebClient.create(address, setupProviders(), "alice", "security", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    // Get Authorization Code
    String code = getAuthorizationCode(client);
    assertNotNull(code);
    // Now get the access token
    client = WebClient.create(address, setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
    // Save the Cookie for the second request...
    WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
    ClientAccessToken accessToken = getAccessTokenWithAuthorizationCode(client, code);
    assertNotNull(accessToken.getTokenKey());
}
Also used : ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Example 15 with Client

use of org.apache.cxf.rs.security.oauth2.common.Client in project testcases by coheigea.

the class AuthorizationGrantTest method testClientCredentialsGrant.

@org.junit.Test
public void testClientCredentialsGrant() throws Exception {
    URL busFile = AuthorizationGrantTest.class.getResource("cxf-client.xml");
    String address = "https://localhost:" + PORT + "/services/";
    WebClient client = WebClient.create(address, setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
    // Get Access Token
    client.type("application/x-www-form-urlencoded").accept("application/json");
    client.path("token");
    Form form = new Form();
    form.param("grant_type", "client_credentials");
    Response response = client.post(form);
    ClientAccessToken accessToken = response.readEntity(ClientAccessToken.class);
    assertNotNull(accessToken.getTokenKey());
    assertNotNull(accessToken.getRefreshToken());
}
Also used : Response(javax.ws.rs.core.Response) Form(javax.ws.rs.core.Form) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Aggregations

WebClient (org.apache.cxf.jaxrs.client.WebClient)112 ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)100 Response (javax.ws.rs.core.Response)79 Client (org.apache.cxf.rs.security.oauth2.common.Client)75 Form (javax.ws.rs.core.Form)64 URL (java.net.URL)59 OAuthAuthorizationData (org.apache.cxf.rs.security.oauth2.common.OAuthAuthorizationData)36 ServerAccessToken (org.apache.cxf.rs.security.oauth2.common.ServerAccessToken)36 Test (org.junit.Test)35 OAuthServiceException (org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException)27 UserSubject (org.apache.cxf.rs.security.oauth2.common.UserSubject)25 AccessTokenRegistration (org.apache.cxf.rs.security.oauth2.common.AccessTokenRegistration)22 OAuthPermission (org.apache.cxf.rs.security.oauth2.common.OAuthPermission)21 JwsJwtCompactConsumer (org.apache.cxf.rs.security.jose.jws.JwsJwtCompactConsumer)16 JwtToken (org.apache.cxf.rs.security.jose.jwt.JwtToken)15 ArrayList (java.util.ArrayList)13 TokenIntrospection (org.apache.cxf.rs.security.oauth2.common.TokenIntrospection)12 RefreshToken (org.apache.cxf.rs.security.oauth2.tokens.refresh.RefreshToken)12 Book (org.apache.cxf.systest.jaxrs.security.Book)11 Consumes (javax.ws.rs.Consumes)8