Package com.github.zhangkaitao.shiro.chapter18.oauth2

Examples of com.github.zhangkaitao.shiro.chapter18.oauth2.OAuth2Token


    return ret;
  }

  public OAuth2Token removeToken(final OAuth2Token token) {
    OAuth2Token ret = null;
    final String tokenKey = this.getTokenKey(token);
    if (tokenKey != null) {
      ret = this.getTokenMap().remove(tokenKey);
    }
View Full Code Here


            final String macAlgorithm = msg.getMacAlgorithm();
            final String macSecret = msg.getMacSecret();
            final Map<String, String> unparsedProperties = msg.getUnparsedProperties();

            if (accessToken != null) {
              final OAuth2Token storedAccessToken = this.store.createToken();
              storedAccessToken.setIssuedAt(issuedAt);
              if (expiresIn != null) {
                storedAccessToken.setExpiresAt(issuedAt + Long.decode(expiresIn) * 1000);
              } else {
                storedAccessToken.setExpiresAt(0);
              }
              storedAccessToken.setGadgetUri(gadgetUri);
              storedAccessToken.setServiceName(providerName);
              storedAccessToken.setScope(scope);
              storedAccessToken.setSecret(accessToken.getBytes("UTF-8"));
              storedAccessToken.setTokenType(tokenType);
              storedAccessToken.setType(OAuth2Token.Type.ACCESS);
              storedAccessToken.setUser(user);
              if (macAlgorithm != null) {
                storedAccessToken.setMacAlgorithm(macAlgorithm);
              }
              if (macSecret != null) {
                storedAccessToken.setMacSecret(macSecret.getBytes("UTF-8"));
              }
              storedAccessToken.setProperties(unparsedProperties);
              this.store.setToken(storedAccessToken);
              accessor.setAccessToken(storedAccessToken);
            }

            if (refreshToken != null) {
              final OAuth2Token storedRefreshToken = this.store.createToken();
              storedRefreshToken.setExpiresAt(0);
              storedRefreshToken.setGadgetUri(gadgetUri);
              storedRefreshToken.setServiceName(providerName);
              storedRefreshToken.setScope(scope);
              storedRefreshToken.setSecret(refreshToken.getBytes("UTF-8"));
              storedRefreshToken.setTokenType(tokenType);
              storedRefreshToken.setType(OAuth2Token.Type.REFRESH);
              storedRefreshToken.setUser(user);
              this.store.setToken(storedRefreshToken);
              accessor.setRefreshToken(storedRefreshToken);
            }
          }
        }
View Full Code Here

      if (unAuthorizedRequestUri == null) {
        return BearerTokenHandler.getError("unAuthorizedRequestUri is null");
      }

      final OAuth2Token accessToken = accessor.getAccessToken();

      if (accessToken == null || accessToken.getTokenType().length() == 0) {
        return BearerTokenHandler.getError("accessToken is invalid " + accessToken);
      }

      if (!BearerTokenHandler.TOKEN_TYPE.equalsIgnoreCase(accessToken.getTokenType())) {
        return BearerTokenHandler.getError("token type mismatch expected "
                + BearerTokenHandler.TOKEN_TYPE + " but got " + accessToken.getTokenType());
      }

      if (accessor.isUrlParameter()) {
        final Map<String, String> queryParams = Maps.newHashMap();
        final byte[] secretBytes = accessToken.getSecret();
        final String secret = new String(secretBytes, "UTF-8");
        queryParams.put(OAuth2Message.ACCESS_TOKEN, secret);
        final String authorizedUriString = OAuth2Utils.buildUrl(unAuthorizedRequestUri.toString(),
                queryParams, null);

        request.setUri(Uri.parse(authorizedUriString));
      }

      if (accessor.isAuthorizationHeader()) {
        request.setHeader("Authorization", BearerTokenHandler.TOKEN_TYPE + ' '
                + new String(accessToken.getSecret(), "UTF-8"));
      }

      return null;
    } catch (final Exception e) {
      return BearerTokenHandler.getError("Exception occurred " + e.getMessage(), e);
View Full Code Here

    Assert.assertNull(result);
  }

  @Test
  public void testGetToken_1() throws Exception {
    final OAuth2Token result = this.cache.getToken(MockUtils.GADGET_URI1, MockUtils.SERVICE_NAME,
            MockUtils.USER, MockUtils.SCOPE, Type.ACCESS);

    Assert.assertNotNull(result);
    Assert.assertEquals(MockUtils.ACCESS_SECRET, new String(result.getSecret(), "UTF-8"));
  }
View Full Code Here

  }

  @Test
  public void testRemoveToken_1() throws Exception {

    OAuth2Token result = this.cache.getToken(MockUtils.GADGET_URI1, MockUtils.SERVICE_NAME,
            MockUtils.USER, MockUtils.SCOPE, Type.ACCESS);

    Assert.assertNotNull(result);

    result = this.cache.removeToken(result);
View Full Code Here

    Assert.assertEquals("AAA", accessor.getRedirectUri());
  }

  @Test
  public void testStoreToken_1() throws Exception {
    OAuth2Token token = new OAuth2TokenPersistence(MockUtils.getDummyEncrypter());
    token.setGadgetUri("xxx");
    token.setServiceName("yyy");
    token.setExpiresAt(2);
    token.setIssuedAt(1);
    token.setMacAlgorithm(OAuth2Message.HMAC_SHA_1);
    token.setMacSecret("shh, it's a secret".getBytes("UTF-8"));
    token.setScope("mac_scope");
    token.setSecret("i'll never tell".getBytes("UTF-8"));
    token.setTokenType(OAuth2Message.MAC_TOKEN_TYPE);
    token.setType(OAuth2Token.Type.ACCESS);
    token.setUser("zzz");

    this.cache.storeToken(token);

    token = this.cache.getToken(token.getGadgetUri(), token.getServiceName(), token.getUser(),
            token.getScope(), token.getType());

    Assert.assertNotNull(token);
    Assert.assertEquals("xxx", token.getGadgetUri());
    Assert.assertEquals("yyy", token.getServiceName());

    Assert.assertEquals(2, token.getExpiresAt());
    Assert.assertEquals(1, token.getIssuedAt());
    Assert.assertEquals(OAuth2Message.HMAC_SHA_1, token.getMacAlgorithm());
    Assert.assertEquals("shh, it's a secret", new String(token.getMacSecret(), "UTF-8"));
    Assert.assertEquals("mac_scope", token.getScope());
    Assert.assertEquals("i'll never tell", new String(token.getSecret(), "UTF-8"));
    Assert.assertEquals(OAuth2Message.MAC_TOKEN_TYPE, token.getTokenType());
    Assert.assertEquals(OAuth2Token.Type.ACCESS, token.getType());
    Assert.assertEquals("zzz", token.getUser());
  }
View Full Code Here

  public void testStoreTokens_1() throws Exception {
    this.cache.clearTokens();

    final Collection<OAuth2Token> tokens = new HashSet<OAuth2Token>(2);

    final OAuth2Token accessToken = MockUtils.getAccessToken();
    final OAuth2Token refreshToken = MockUtils.getRefreshToken();

    tokens.add(accessToken);
    tokens.add(refreshToken);

    this.cache.storeTokens(tokens);

    Assert.assertNotNull(this.cache.getToken(accessToken.getGadgetUri(),
            accessToken.getServiceName(), accessToken.getUser(), accessToken.getScope(),
            accessToken.getType()));
    Assert.assertNotNull(this.cache.getToken(refreshToken.getGadgetUri(),
            refreshToken.getServiceName(), refreshToken.getUser(), refreshToken.getScope(),
            refreshToken.getType()));
  }
View Full Code Here

    final OAuth2HandlerError result = fixture.handleRequest(accessor, request);

    Assert.assertNull(result);

    final OAuth2Token accessToken = CodeAuthorizationResponseHandlerTest.store.getToken(
        accessor.getGadgetUri(), accessor.getServiceName(), accessor.getUser(),
        accessor.getScope(), OAuth2Token.Type.ACCESS);
    Assert.assertNotNull(accessToken);
    Assert.assertEquals("xxx", new String(accessToken.getSecret(), "UTF-8"));
    Assert.assertEquals(OAuth2Message.BEARER_TOKEN_TYPE, accessToken.getTokenType());
    Assert.assertTrue(accessToken.getExpiresAt() > 1000);

    final OAuth2Token refreshToken = CodeAuthorizationResponseHandlerTest.store.getToken(
        accessor.getGadgetUri(), accessor.getServiceName(), accessor.getUser(),
        accessor.getScope(), OAuth2Token.Type.REFRESH);
    Assert.assertNotNull(refreshToken);
    Assert.assertEquals("yyy", new String(refreshToken.getSecret(), "UTF-8"));
  }
View Full Code Here

    final OAuth2HandlerError result = fixture.handleRequest(accessor, request);

    Assert.assertNull(result);

    final OAuth2Token accessToken = CodeAuthorizationResponseHandlerTest.store.getToken(
        accessor.getGadgetUri(), accessor.getServiceName(), accessor.getUser(),
        accessor.getScope(), OAuth2Token.Type.ACCESS);
    Assert.assertNotNull(accessToken);
    Assert.assertEquals("xxx", new String(accessToken.getSecret(), "UTF-8"));
    Assert.assertEquals(OAuth2Message.BEARER_TOKEN_TYPE, accessToken.getTokenType());
    Assert.assertTrue(accessToken.getExpiresAt() > 1000);

    final OAuth2Token refreshToken = CodeAuthorizationResponseHandlerTest.store.getToken(
        accessor.getGadgetUri(), accessor.getServiceName(), accessor.getUser(),
        accessor.getScope(), OAuth2Token.Type.REFRESH);
    Assert.assertNotNull(refreshToken);
    Assert.assertEquals("yyy", new String(refreshToken.getSecret(), "UTF-8"));

    Assert.assertNotNull( fetcher.request );

    SecurityToken st = fetcher.request.getSecurityToken();
    Assert.assertNotNull( st );
View Full Code Here

    final OAuth2HandlerError result = TokenAuthorizationResponseHandlerTest.tarh.handleResponse(
            accessor, response);

    Assert.assertNull(result);

    final OAuth2Token accessToken = TokenAuthorizationResponseHandlerTest.store.getToken(
            accessor.getGadgetUri(), accessor.getServiceName(), accessor.getUser(),
            accessor.getScope(), OAuth2Token.Type.ACCESS);
    Assert.assertNotNull(accessToken);
    Assert.assertEquals("xxx", new String(accessToken.getSecret(), "UTF-8"));
    Assert.assertEquals(OAuth2Message.BEARER_TOKEN_TYPE, accessToken.getTokenType());
    Assert.assertTrue(accessToken.getExpiresAt() > 1000);

    final OAuth2Token refreshToken = TokenAuthorizationResponseHandlerTest.store.getToken(
            accessor.getGadgetUri(), accessor.getServiceName(), accessor.getUser(),
            accessor.getScope(), OAuth2Token.Type.REFRESH);
    Assert.assertNotNull(refreshToken);
    Assert.assertEquals("yyy", new String(refreshToken.getSecret(), "UTF-8"));
  }
View Full Code Here

TOP

Related Classes of com.github.zhangkaitao.shiro.chapter18.oauth2.OAuth2Token

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.