Java Code Examples for org.apache.cxf.jaxrs.client.WebClient#authorization()

The following examples show how to use org.apache.cxf.jaxrs.client.WebClient#authorization() . 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: PushNotificationServiceImpl.java    From g-suite-identity-sync with Apache License 2.0 6 votes vote down vote up
private void startPushChannel(String hostname) {
    WebClient webClient = WebClient.fromClient(directoryApiClient, true)
            .path("/admin/reports/v1/activity/users/all/applications/admin/watch");
    ClientAccessToken accessToken = tokenCache.getToken();
    webClient.authorization(accessToken);
    String url = "https://" + hostname + "/cxf/push/notify";
    StartPushChannel watchRequest = new StartPushChannel(url, Duration.ofHours(6));
    try {
        PushChannel ch = webClient.post(watchRequest, PushChannel.class);
        channel = Optional.of(ch);
        store(ch);
    } catch (ClientErrorException e) {
        String body = e.getResponse().readEntity(String.class);
        log.error("Cannot register push notification channel for {}.\nResponse: {}", config.getGSuiteDomain(), body);
        throw new RuntimeException("Cannot register push notification channel for " + hostname);
    }
}
 
Example 2
Source File: GSuiteDirectoryServiceImpl.java    From g-suite-identity-sync with Apache License 2.0 6 votes vote down vote up
private GroupMembership readGroupMembers(String groupKey, GroupMembership parent) throws ResourceNotFoundException {
    String path = MessageFormat.format("/admin/directory/v1/groups/{0}/members", new Object[]{groupKey});

    WebClient webClient = WebClient.fromClient(directoryApiClient, true).path(path);
    ClientAccessToken accessToken = tokenCache.getToken();
    webClient.authorization(accessToken);
    GroupMembership result;
    try {
        if (parent != null && parent.getNextPageToken() != null) {
            result = webClient.query("pageToken", parent.getNextPageToken()).get(GroupMembership.class);
            result.getMembers().addAll(parent.getMembers());
        } else {
            result = webClient.get(GroupMembership.class);
        }
        return result.getNextPageToken() != null ? readGroupMembers(groupKey, result) : result;
    } catch (NotFoundException e) {
        throw new ResourceNotFoundException("Group " + groupKey + " not found.", e);
    }
}
 
Example 3
Source File: BigQueryService.java    From cxf with Apache License 2.0 6 votes vote down vote up
static List<ShakespeareText> getMatchingTexts(WebClient bqClient, ClientAccessToken accessToken,
                                              String searchWord, String maxResults) {
    bqClient.authorization(accessToken);
    String bigQuerySelect = String.format(BQ_SELECT, searchWord);
    String bigQueryRequest = String.format(BQ_REQUEST, bigQuerySelect, Integer.parseInt(maxResults));

    JsonMapObject jsonMap = bqClient.post(bigQueryRequest, JsonMapObject.class);

    List<ShakespeareText> texts = new LinkedList<>();
    List<Map<String, Object>> rows = CastUtils.cast((List<?>)jsonMap.getProperty("rows"));
    if (rows != null) {
        for (Map<String, Object> row : rows) {
            List<Map<String, Object>> fields = CastUtils.cast((List<?>)row.get("f"));
            ShakespeareText text = new ShakespeareText((String)fields.get(0).get("v"),
                                                       (String)fields.get(1).get("v"));
            texts.add(text);
        }
    }
    return texts;
}
 
Example 4
Source File: JAXRSOAuth2TlsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testRegisterClientTwoWayTLSClientIdBoundDynReg() throws Exception {
    String dynRegAddress = "https://localhost:" + PORT + "/oauth2Jwt/register";
    WebClient wcDynReg = createDynRegWebClient(dynRegAddress);

    wcDynReg.accept("application/json").type("application/json");
    ClientRegistration reg = newClientRegistration();
    wcDynReg.authorization(new ClientAccessToken("Bearer", "123456789"));
    ClientRegistrationResponse resp = wcDynReg.post(reg, ClientRegistrationResponse.class);

    doTestTwoWayTLSClientIdBoundJwt(resp.getClientId());

    // delete the client
    String regAccessToken = resp.getRegistrationAccessToken();
    assertNotNull(regAccessToken);
    wcDynReg.path(resp.getClientId());


    wcDynReg.authorization(new ClientAccessToken("Bearer", regAccessToken));

    assertEquals(200, wcDynReg.delete().getStatus());
    assertNotNull(regAccessToken);
}
 
Example 5
Source File: PushNotificationServiceImpl.java    From g-suite-identity-sync with Apache License 2.0 5 votes vote down vote up
private void stopPushChannel(StopPushChannel stopChannel) {
    WebClient webClient = WebClient.fromClient(directoryApiClient, true).path("/admin/reports_v1/channels/stop");
    ClientAccessToken accessToken = tokenCache.getToken();
    webClient.authorization(accessToken);
    Response resp = webClient.post(stopChannel);
    Response.StatusType status = resp.getStatusInfo();
    if (status.toEnum() == OK || status.toEnum() == NO_CONTENT || status.toEnum() == NOT_FOUND) {
        log.info("Push notifications successfully stopped");
        this.channel = empty();
        config.getPushChannelFile().delete();
    } else {
        log.error("Cannot stop watching domain! Status: {}, Reason: {}", status.getStatusCode(), status.getReasonPhrase());
        throw new RuntimeException("Cannot disable notification channel");
    }
}
 
Example 6
Source File: GSuiteDirectoryServiceImpl.java    From g-suite-identity-sync with Apache License 2.0 5 votes vote down vote up
@Override
public GSuiteGroup getGroup(String groupKey) {
    String path = MessageFormat.format("/admin/directory/v1/groups/{0}", new Object[]{groupKey});

    WebClient webClient = WebClient.fromClient(directoryApiClient, true);
    webClient.authorization(tokenCache.getToken());
    GSuiteGroup group = webClient.path(path).get(GSuiteGroup.class);
    return group;
}
 
Example 7
Source File: GSuiteDirectoryServiceImpl.java    From g-suite-identity-sync with Apache License 2.0 5 votes vote down vote up
@Override
public GroupList getUserGroups(String userKey) {
    WebClient webClient = WebClient.fromClient(directoryApiClient, true).path("/admin/directory/v1/groups");
    webClient.authorization(tokenCache.getToken());
    if (userKey != null) {
        webClient.query("userKey", userKey);
    }
    GroupList groupList = webClient.query("domain", config.getGSuiteDomain()).get(GroupList.class);
    return groupList;
}
 
Example 8
Source File: GSuiteDirectoryServiceImpl.java    From g-suite-identity-sync with Apache License 2.0 5 votes vote down vote up
@Override
public GSuiteUser getUser(String userKey) {
    String path = MessageFormat.format("/admin/directory/v1/users/{0}", new Object[]{userKey});
    WebClient webClient = WebClient.fromClient(directoryApiClient, true);

    ClientAccessToken accessToken = tokenCache.getToken();
    webClient.authorization(accessToken);
    GSuiteUser user = webClient.path(path).get(GSuiteUser.class);
    return user;
}
 
Example 9
Source File: GSuiteDirectoryServiceImpl.java    From g-suite-identity-sync with Apache License 2.0 5 votes vote down vote up
@Override
public void updateUserPassword(String userKey, String password) throws InvalidPasswordException {
    String path = MessageFormat.format("/admin/directory/v1/users/{0}", new Object[]{userKey});
    WebClient webClient = WebClient.fromClient(directoryApiClient, true);

    ClientAccessToken accessToken = tokenCache.getToken();
    webClient.authorization(accessToken);
    GSuiteUser user = new GSuiteUser();
    user.setPassword(password);
    Response response = webClient.path(path).put(user);
    if (response.getStatus() != Response.Status.OK.getStatusCode()) {
        throw new InvalidPasswordException("Can't change password. Response: " + response.readEntity(String.class));
    }
}
 
Example 10
Source File: GSuiteDirectoryServiceImpl.java    From g-suite-identity-sync with Apache License 2.0 5 votes vote down vote up
private GSuiteUsers readAllUsers(GSuiteUsers parent) {
    WebClient webClient = WebClient.fromClient(directoryApiClient, true).path("/admin/directory/v1/users");
    ClientAccessToken accessToken = tokenCache.getToken();
    webClient.authorization(accessToken);
    GSuiteUsers result;
    webClient.query("domain", config.getGSuiteDomain());
    if (parent != null && parent.getNextPageToken() != null) {
        result = webClient.query("pageToken", parent.getNextPageToken()).get(GSuiteUsers.class);
        result.getUsers().addAll(parent.getUsers());
    } else {
        result = webClient.get(GSuiteUsers.class);
    }
    return result.getNextPageToken() != null ? readAllUsers(result) : result;
}
 
Example 11
Source File: OIDCDynamicRegistrationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testUpdateClient() throws Exception {
    URL busFile = OIDCDynamicRegistrationTest.class.getResource("client.xml");
    String address = "https://localhost:" + DYNREG_SERVER.getPort() + "/services/dynamicWithAt/register";
    WebClient wc =
        WebClient.create(address, Collections.singletonList(new JsonMapObjectProvider()), busFile.toString())
        .accept("application/json").type("application/json")
        .authorization(new ClientAccessToken(OAuthConstants.BEARER_AUTHORIZATION_SCHEME, ACCESS_TOKEN));

    final ClientRegistration reg = newClientRegistrationCodeGrant();
    final ClientRegistrationResponse clientRegistrationResponse = wc
        .post(reg, ClientRegistrationResponse.class);

    final String regAccessToken = clientRegistrationResponse.getRegistrationAccessToken();
    assertNotNull(regAccessToken);

    reg.setScope(OidcUtils.getEmailScope());
    final ClientRegistration updatedClientRegistration = wc.path(clientRegistrationResponse.getClientId())
        .authorization(new ClientAccessToken(OAuthConstants.BEARER_AUTHORIZATION_SCHEME, regAccessToken))
        .put(reg, ClientRegistration.class);

    assertEquals(OidcUtils.getEmailScope(), updatedClientRegistration.getScope());
    // https://tools.ietf.org/html/rfc7592#section-2.2
    assertNull(updatedClientRegistration.getProperty("registration_access_token"));
    assertNull(updatedClientRegistration.getProperty("registration_client_uri"));
    assertNull(updatedClientRegistration.getProperty("client_secret_expires_at"));
    assertNull(updatedClientRegistration.getProperty("client_id_issued_at"));

    wc.authorization(null);

    assertEquals(Status.UNAUTHORIZED.getStatusCode(),
        wc.put(reg).getStatus());
    assertEquals(Status.UNAUTHORIZED.getStatusCode(),
        wc.delete().getStatus());

    wc.authorization(new ClientAccessToken(OAuthConstants.BEARER_AUTHORIZATION_SCHEME, regAccessToken));
    assertEquals(200, wc.delete().getStatus());
}
 
Example 12
Source File: JAXRSOAuth2TlsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private WebClient createRsWebClient(String address, ClientAccessToken at, String clientContext) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSOAuth2TlsTest.class.getResource(clientContext);
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);

    WebClient wc = bean.createWebClient();
    wc.accept(MediaType.APPLICATION_XML);
    wc.authorization(at);
    return wc;
}