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

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


    this.persister = MockUtils.getDummyPersister();
  }

  @Test
  public void testCreateToken_1() throws Exception {
    final OAuth2Token result = this.persister.createToken();

    Assert.assertNotNull(result);
  }
View Full Code Here


      return true;
    }
    if (!(obj instanceof OAuth2Token)) {
      return false;
    }
    final OAuth2Token other = (OAuth2Token) obj;
    if (this.gadgetUri == null) {
      if (other.getGadgetUri() != null) {
        return false;
      }
    } else if (!this.gadgetUri.equals(other.getGadgetUri())) {
      return false;
    }
    if (this.serviceName == null) {
      if (other.getServiceName() != null) {
        return false;
      }
    } else if (!this.serviceName.equals(other.getServiceName())) {
      return false;
    }

    if (this.user == null) {
      if (other.getUser() != null) {
        return false;
      }
    } else if (!this.user.equals(other.getUser())) {
      return false;
    }
    if (this.scope == null) {
      if (other.getScope() != null) {
        return false;
      }
    } else if (!this.scope.equals(other.getScope())) {
      return false;
    }
    if (this.type == null) {
      if (other.getType() != null) {
        return false;
      }
    } else if (!this.type.equals(other.getType())) {
      return false;
    }

    return true;
  }
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));
              } 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

      final boolean lastAttempt) throws OAuth2RequestException {
    final String method = "fetchFromServer";
    log.entering(CLASS, method, new Object[] { accessor, lastAttempt });
    HttpResponse ret;
    final long currentTime = System.currentTimeMillis();
    OAuth2Token accessToken = accessor.getAccessToken();
    if (accessToken != null) {
      final long expiresAt = accessToken.getExpiresAt();
      if (expiresAt != 0) {
        if (currentTime >= expiresAt) {
          if (log.isLoggable(Level.FINEST)) {
            log.logp(Level.FINEST, CLASS, method, "accessToken has expired at {0}", new Object[]{expiresAt});
          }
          try {
            this.tokenStore.removeAccessToken(accessor);
          } catch (final GadgetException e) {
            throw new OAuth2RequestException(OAuth2Error.MISSING_SERVER_RESPONSE,
                "error removing access_token", null);
          }
          accessToken = null;
          accessor.setAccessToken(null);
          if (!lastAttempt) {
            return null;
          }
        }
      }
    }

    OAuth2Token refreshToken = accessor.getRefreshToken();
    if (refreshToken != null) {
      final long expiresAt = refreshToken.getExpiresAt();
      if (expiresAt != 0) {
        if (currentTime >= expiresAt) {
          if (log.isLoggable(Level.FINEST)) {
            log.logp(Level.FINEST, CLASS, method, "refreshToken has expired at {0}", new Object[]{expiresAt});
          }
View Full Code Here

        contextMessage, errorUri, errorDescription);
    return responseBuilder.create();
  }

  private static boolean haveAccessToken(final OAuth2Accessor accessor) {
    OAuth2Token token = accessor.getAccessToken();
    return token != null && DominoOAuth2Request.validateAccessToken(token);
  }
View Full Code Here

    OAuth2Token token = accessor.getAccessToken();
    return token != null && DominoOAuth2Request.validateAccessToken(token);
  }

  private static boolean haveRefreshToken(final OAuth2Accessor accessor) {
    OAuth2Token token = accessor.getRefreshToken();
    return token != null && DominoOAuth2Request.validateRefreshToken(token);
  }
View Full Code Here

        client);
  }
 
  private DominoOAuth2Accessor createAccessor(String gadgetUri, String serviceName, String user, String scope, String container,
      DominoOAuth2Client client) throws GadgetException {
    final OAuth2Token accessToken = this.getAccessToken(gadgetUri, serviceName, user, scope, container);
    final OAuth2Token refreshToken = this.getRefreshToken(gadgetUri, serviceName, user, scope, container);
    String authType = client.getClientAuthenticationType() == null ? null : client.getClientAuthenticationType().toString();
    final BasicDominoOAuth2Accessor newAccessor = new BasicDominoOAuth2Accessor(gadgetUri, serviceName,
        user, scope, client.isAllowModuleOverride(), this.stateCrypter, this.globalRedirectUri,
        this.authority, this.contextRoot, container);
    newAccessor.setAccessToken(accessToken);
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.storeAccessToken(accessor.getContainer(), 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.storeRefreshToken(accessor.getContainer(), storedRefreshToken);
              accessor.setRefreshToken(storedRefreshToken);
            }
          }
        }
View Full Code Here

      return true;
    }
    if (!(obj instanceof OAuth2Token)) {
      return false;
    }
    final OAuth2Token other = (OAuth2Token) obj;
    if (this.gadgetUri == null) {
      if (other.getGadgetUri() != null) {
        return false;
      }
    } else if (!this.gadgetUri.equals(other.getGadgetUri())) {
      return false;
    }
    if (this.serviceName == null) {
      if (other.getServiceName() != null) {
        return false;
      }
    } else if (!this.serviceName.equals(other.getServiceName())) {
      return false;
    }

    if (this.user == null) {
      if (other.getUser() != null) {
        return false;
      }
    } else if (!this.user.equals(other.getUser())) {
      return false;
    }
    if (this.scope == null) {
      if (other.getScope() != null) {
        return false;
      }
    } else if (!this.scope.equals(other.getScope())) {
      return false;
    }
    if (this.type == null) {
      if (other.getType() != null) {
        return false;
      }
    } else if (!this.type.equals(other.getType())) {
      return false;
    }

    return true;
  }
View Full Code Here

      final OAuth2HandlerError handlerError = MacTokenHandler.validateOAuth2Params(accessor, request);
      if (handlerError != null) {
        return handlerError;
      }

      final OAuth2Token accessToken = accessor.getAccessToken();

      String ext = accessToken.getMacExt();
      if ((ext == null) || (ext.length() == 0)) {
        ext = "";
      }

      // REQUIRED. The MAC key identifier.
      final String id = new String(accessToken.getSecret(), "UTF-8");

      // REQUIRED. A unique string generated by the client to allow the
      // server to verify that a request has never been made before and
      // helps prevent replay attacks when requests are made over an
      // insecure channel. The nonce value MUST be unique across all
      // requests with the same MAC key identifier.
      // The nonce value MUST consist of the age of the MAC credentials
      // expressed as the number of seconds since the credentials were
      // issued to the client, a colon character (%x25), and a unique
      // string (typically random). The age value MUST be a positive
      // integer and MUST NOT include leading zeros (e.g.
      // "000137131200"). For example: "273156:di3hvdf8".
      // To avoid the need to retain an infinite number of nonce values
      // for future checks, the server MAY choose to restrict the time
      // period after which a request with an old age is rejected. If
      // such a restriction is enforced, the server SHOULD allow for a
      // sufficiently large window to accommodate network delays which
      // will affect the credentials issue time used by the client to
      // calculate the credentials' age.
      final long currentTime = System.currentTimeMillis() / 1000;
      final String nonce = Long.toString(currentTime - accessToken.getIssuedAt()) + ':'
          + String.valueOf(Math.abs(Crypto.RAND.nextLong()));

      // OPTIONAL. The HTTP request payload body hash as described in
      // Section 3.2.

      String bodyHash = MacTokenHandler.getBodyHash(request, accessToken.getMacSecret(),
          accessToken.getMacAlgorithm());
      if (bodyHash == null) {
        bodyHash = "";
      }

      // mac
      // REQUIRED. The HTTP request MAC as described in Section 3.3.
      final Uri uri = request.getUri();

      String uriString = uri.getPath();
      if (uri.getQuery() != null) {
        uriString = uriString + '?' + uri.getQuery();
      }

      String host = uri.getAuthority();
      String port = "80";
      final int index = host.indexOf(':');
      if (index > 0) {
        port = host.substring(index + 1);
        host = host.substring(0, index);
      } else {
        final String scheme = uri.getScheme();
        if ("https".equals(scheme)) {
          port = "443";
        }
      }

      final String mac = MacTokenHandler.getMac(nonce, request.getMethod(), uriString, host, port,
          bodyHash, ext, accessToken.getMacSecret(), accessToken.getMacAlgorithm());

      final String headerString = buildHeaderString(id, nonce, bodyHash, ext, mac);

      request.setHeader(OAuth2Message.AUTHORIZATION_HEADER, headerString);
      return null;
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.