protected void saveAccessToken(ServerAccessToken serverToken) {
    getEntityManager().getTransaction().begin();
    List<OAuthPermission> perms = new LinkedList<OAuthPermission>();
    for (OAuthPermission perm : serverToken.getScopes()) {
      OAuthPermission permSaved =
          getEntityManager().find(OAuthPermission.class, perm.getPermission());
      if (permSaved != null) {
        perms.add(permSaved);
      } else {
        getEntityManager().persist(perm);
        perms.add(perm);
      }
    }
    serverToken.setScopes(perms);

    UserSubject sub =
        getEntityManager().find(UserSubject.class, serverToken.getSubject().getLogin());
    if (sub == null) {
      getEntityManager().persist(serverToken.getSubject());
    } else {
      sub = getEntityManager().merge(serverToken.getSubject());
      serverToken.setSubject(sub);
    }

    getEntityManager().persist(serverToken);
    getEntityManager().getTransaction().commit();
  }
  @Override
  public ServerAccessToken createAccessToken(final AccessTokenRegistration atr)
      throws OAuthServiceException {
    token = new BearerAccessToken(atr.getClient(), 3600L);

    final List<String> scope =
        atr.getApprovedScope().isEmpty() ? atr.getRequestedScope() : atr.getApprovedScope();
    token.setScopes(convertScopeToPermissions(atr.getClient(), scope));
    token.setSubject(atr.getSubject());
    token.setGrantType(atr.getGrantType());

    return token;
  }
Beispiel #3
0
  private static ServerAccessToken recreateAccessToken(
      OAuthDataProvider provider, String newTokenKey, String[] parts) {

    @SuppressWarnings("serial")
    final ServerAccessToken newToken =
        new ServerAccessToken(
            provider.getClient(parts[4]),
            parts[1],
            newTokenKey == null ? parts[0] : newTokenKey,
            Long.valueOf(parts[2]),
            Long.valueOf(parts[3])) {
          //
        };

    newToken.setRefreshToken(getStringPart(parts[5]));
    newToken.setGrantType(getStringPart(parts[6]));
    newToken.setAudience(getStringPart(parts[7]));
    newToken.setParameters(parseSimpleMap(parts[8]));

    // Permissions
    if (!parts[9].trim().isEmpty()) {
      List<OAuthPermission> perms = new LinkedList<OAuthPermission>();
      String[] allPermParts = parts[9].split("&");
      for (int i = 0; i + 4 < allPermParts.length; i = i + 5) {
        OAuthPermission perm = new OAuthPermission(allPermParts[i], allPermParts[i + 1]);
        perm.setDefault(Boolean.valueOf(allPermParts[i + 2]));
        perm.setHttpVerbs(parseSimpleList(allPermParts[i + 3]));
        perm.setUris(parseSimpleList(allPermParts[i + 4]));
        perms.add(perm);
      }
      newToken.setScopes(perms);
    }
    // UserSubject:
    newToken.setSubject(recreateUserSubject(parts[10]));

    return newToken;
  }