Example usage for org.springframework.security.oauth2.common OAuth2AccessToken getValue

List of usage examples for org.springframework.security.oauth2.common OAuth2AccessToken getValue

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.common OAuth2AccessToken getValue.

Prototype

String getValue();

Source Link

Usage

From source file:org.cloudfoundry.client.lib.oauth2.OauthClient.java

public String getAuthorizationHeader() {
    OAuth2AccessToken accessToken = getToken();
    if (accessToken != null) {
        return accessToken.getTokenType() + " " + accessToken.getValue();
    }/*from  w  ww .  ja  v  a 2 s . c om*/
    return null;
}

From source file:eu.trentorise.smartcampus.permissionprovider.oauth.NonRemovingTokenServices.java

@Transactional(isolation = Isolation.SERIALIZABLE)
public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException {
    OAuth2AccessToken res = super.createAccessToken(authentication);
    traceUserLogger.info(/*from   www . j av  a  2 s . com*/
            String.format("'type':'new','user':'%s','token':'%s'", authentication.getName(), res.getValue()));
    return res;
}

From source file:org.zalando.stups.oauth2.spring.client.SecurityContextTokenProviderTest.java

@Test
public void testObtainAccessToken() throws Exception {
    final OAuth2AccessToken oAuth2AccessToken = tokenProvider
            .obtainAccessToken(mock(OAuth2ProtectedResourceDetails.class), mock(AccessTokenRequest.class));
    assertThat(oAuth2AccessToken).isNotNull();
    assertThat(oAuth2AccessToken.getValue()).isEqualTo("1234567890");
}

From source file:org.springframework.security.oauth2.common.OAuth2AccessTokenJackson2Serializer.java

@Override
public void serialize(OAuth2AccessToken token, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonGenerationException {
    jgen.writeStartObject();/*from ww  w  . java2s.c o m*/
    jgen.writeStringField(OAuth2AccessToken.ACCESS_TOKEN, token.getValue());
    jgen.writeStringField(OAuth2AccessToken.TOKEN_TYPE, token.getTokenType());
    OAuth2RefreshToken refreshToken = token.getRefreshToken();
    if (refreshToken != null) {
        jgen.writeStringField(OAuth2AccessToken.REFRESH_TOKEN, refreshToken.getValue());
    }
    Date expiration = token.getExpiration();
    if (expiration != null) {
        long now = System.currentTimeMillis();
        jgen.writeNumberField(OAuth2AccessToken.EXPIRES_IN, (expiration.getTime() - now) / 1000);
    }
    Set<String> scope = token.getScope();
    if (scope != null && !scope.isEmpty()) {
        StringBuffer scopes = new StringBuffer();
        for (String s : scope) {
            Assert.hasLength(s, "Scopes cannot be null or empty. Got " + scope + "");
            scopes.append(s);
            scopes.append(" ");
        }
        jgen.writeStringField(OAuth2AccessToken.SCOPE, scopes.substring(0, scopes.length() - 1));
    }
    Map<String, Object> additionalInformation = token.getAdditionalInformation();
    for (String key : additionalInformation.keySet()) {
        jgen.writeObjectField(key, additionalInformation.get(key));
    }
    jgen.writeEndObject();
}

From source file:com.epam.reportportal.auth.store.OAuth2MongoTokenStore.java

@Override
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
    OAuth2AccessTokenEntity tokenEntity = new OAuth2AccessTokenEntity();
    tokenEntity.setTokenId(token.getValue());
    tokenEntity.setToken(SerializationUtils.serialize(token));
    tokenEntity.setAuthentication(SerializationUtils.serialize(authentication));
    tokenEntity.setAuthenticationId(authenticationKeyGenerator.extractKey(authentication));
    tokenEntity.setUserName(authentication.isClientOnly() ? null : authentication.getName());
    tokenEntity.setRefreshToken(null == token.getRefreshToken() ? null : token.getRefreshToken().getValue());
    tokenEntity.setClientId(authentication.getOAuth2Request().getClientId());

    oAuth2AccessTokenRepository.save(tokenEntity);
}

From source file:org.cloudfoundry.identity.api.web.CloudfoundryApiIntegrationTests.java

@Test
public void testClientAccessesProtectedResource() throws Exception {
    OAuth2AccessToken accessToken = context.getAccessToken();
    // add an approval for the scope requested
    HttpHeaders approvalHeaders = new HttpHeaders();
    approvalHeaders.set("Authorization", "bearer " + accessToken.getValue());
    Date oneMinuteAgo = new Date(System.currentTimeMillis() - 60000);
    Date expiresAt = new Date(System.currentTimeMillis() + 60000);
    ResponseEntity<Approval[]> approvals = serverRunning.getRestTemplate().exchange(
            serverRunning.getUrl("/uaa/approvals"), HttpMethod.PUT,
            new HttpEntity<Approval[]>((new Approval[] {
                    new Approval(testAccounts.getUserName(), "app", "cloud_controller.read", expiresAt,
                            ApprovalStatus.APPROVED, oneMinuteAgo),
                    new Approval(testAccounts.getUserName(), "app", "openid", expiresAt,
                            ApprovalStatus.APPROVED, oneMinuteAgo),
                    new Approval(testAccounts.getUserName(), "app", "password.write", expiresAt,
                            ApprovalStatus.APPROVED, oneMinuteAgo) }),
                    approvalHeaders),//from w  w  w.j  ava 2  s .  c  om
            Approval[].class);
    assertEquals(HttpStatus.OK, approvals.getStatusCode());

    // System.err.println(accessToken);
    // The client doesn't know how to use an OAuth bearer token
    CloudFoundryClient client = new CloudFoundryClient("Bearer " + accessToken.getValue(),
            testAccounts.getCloudControllerUrl());
    CloudInfo info = client.getCloudInfo();
    assertNotNull("Wrong cloud info: " + info.getDescription(), info.getUser());
}

From source file:com.haulmont.restapi.auth.ClientProxyTokenStore.java

@Override
public void removeAccessToken(OAuth2AccessToken token) {
    serverTokenStore.removeAccessToken(token.getValue());
}

From source file:sample.jsp.WelcomeController.java

@RequestMapping("/authorization_code")
public String authCode(Map<String, Object> model) throws Exception {
    if (ssoServiceUrl.equals("placeholder")) {
        model.put("header", "Warning: You need to bind to the SSO service.");
        model.put("warning", "Please bind your app to restore regular functionality");
        return "configure_warning";
    }/*  w w  w.  j a  va  2s. co m*/

    Map<?, ?> userInfoResponse = oauth2RestTemplate.getForObject("{ssoServiceUrl}/userinfo", Map.class,
            ssoServiceUrl);
    model.put("ssoServiceUrl", ssoServiceUrl);
    model.put("response", toPrettyJsonString(userInfoResponse));

    OAuth2AccessToken accessToken = oauth2RestTemplate.getOAuth2ClientContext().getAccessToken();
    if (accessToken != null) {
        model.put("access_token", toPrettyJsonString(parseToken(accessToken.getValue())));
        model.put("id_token", toPrettyJsonString(
                parseToken((String) accessToken.getAdditionalInformation().get("id_token"))));
    }

    return "authorization_code";
}

From source file:org.socialhistoryservices.security.MongoTokenStore.java

public OAuth2Authentication readAuthentication(OAuth2AccessToken token) {

    final String tokenValue = token.getValue();
    OAuth2Authentication authentication = (isFresh(tokenValue)) ? this.authenticationTokenStore.get(tokenValue)
            : null;//from  w  w  w  .j  av a  2s. c  o  m
    if (authentication == null) {
        // select token_id, authentication from oauth_access_token where token_id = ?
        final BasicDBObject query = new BasicDBObject();
        query.put("token_id", token.getValue());
        final DBCollection collection = getCollection(OAUTH_ACCESS_TOKEN);
        final DBObject document = collection.findOne(query);
        if (document == null) {
        } else {
            authentication = deserialize((byte[]) document.get("authentication"));
            this.authenticationTokenStore.put(tokenValue, authentication);
            expiration(tokenValue);
        }
    }
    return authentication;
}

From source file:org.zalando.stups.oauth2.spring.client.StupsTokensAccessTokenProviderTest.java

@Test
public void testObtainAccessToken() throws Exception {
    when(mockAccessTokens.getAccessToken(anyString()))
            .thenReturn(new AccessToken("12345", "bearer", 3600, tomorrow()));
    final OAuth2AccessToken accessToken = accessTokenProvider
            .obtainAccessToken(new BaseOAuth2ProtectedResourceDetails(), new DefaultAccessTokenRequest());

    assertThat(accessToken).isNotNull();
    assertThat(accessToken.getValue()).isEqualTo("12345");
    assertThat(accessToken.getTokenType()).isEqualTo("Bearer");
    assertThat(accessToken.isExpired()).isFalse();

    verify(mockAccessTokens).getAccessToken(eq(TOKEN_ID));
}