use of de.ids_mannheim.korap.oauth2.entity.AccessToken in project Kustvakt by KorAP.
the class OpenIdTokenService method createsAccessTokenResponse.
private AccessTokenResponse createsAccessTokenResponse(Authorization authorization) throws KustvaktException {
Set<AccessScope> scopes = authorization.getScopes();
String[] scopeArray = scopes.stream().map(scope -> scope.toString()).toArray(String[]::new);
Scope scope = new Scope(scopeArray);
AccessToken accessToken = new BearerAccessToken(config.getAccessTokenExpiry(), scope);
RefreshToken refreshToken = new RefreshToken();
de.ids_mannheim.korap.oauth2.entity.RefreshToken rt = refreshDao.storeRefreshToken(refreshToken.getValue(), authorization.getUserId(), authorization.getUserAuthenticationTime(), authorization.getClientId(), scopes);
tokenDao.storeAccessToken(accessToken.getValue(), rt, scopes, authorization.getUserId(), authorization.getClientId(), authorization.getUserAuthenticationTime());
return createsAccessTokenResponse(accessToken, refreshToken, scope, authorization.getClientId(), authorization.getUserId(), authorization.getUserAuthenticationTime(), authorization.getNonce());
}
use of de.ids_mannheim.korap.oauth2.entity.AccessToken in project Kustvakt by KorAP.
the class OAuth2ClientService method revokeAllAuthorizationsByClientId.
public void revokeAllAuthorizationsByClientId(String clientId) throws KustvaktException {
// revoke all related authorization codes
List<Authorization> authList = authorizationDao.retrieveAuthorizationsByClientId(clientId);
for (Authorization authorization : authList) {
authorization.setRevoked(true);
authorizationDao.updateAuthorization(authorization);
}
// revoke all related access tokens
List<AccessToken> tokens = tokenDao.retrieveAccessTokenByClientId(clientId, null);
for (AccessToken token : tokens) {
token.setRevoked(true);
tokenDao.updateAccessToken(token);
}
List<RefreshToken> refreshTokens = refreshDao.retrieveRefreshTokenByClientId(clientId, null);
for (RefreshToken token : refreshTokens) {
token.setRevoked(true);
refreshDao.updateRefreshToken(token);
}
}
use of de.ids_mannheim.korap.oauth2.entity.AccessToken in project Kustvakt by KorAP.
the class AccessTokenDao method storeAccessToken.
public void storeAccessToken(String token, RefreshToken refreshToken, Set<AccessScope> scopes, String userId, String clientId, ZonedDateTime authenticationTime) throws KustvaktException {
ParameterChecker.checkStringValue(token, "access_token");
// ParameterChecker.checkObjectValue(refreshToken, "refresh
// token");
ParameterChecker.checkObjectValue(scopes, "scopes");
// ParameterChecker.checkStringValue(userId, "username");
ParameterChecker.checkStringValue(clientId, "client_id");
ParameterChecker.checkObjectValue(authenticationTime, "authentication time");
ZonedDateTime now = ZonedDateTime.now(ZoneId.of(Attributes.DEFAULT_TIME_ZONE));
ZonedDateTime expiry;
AccessToken accessToken = new AccessToken();
if (refreshToken != null) {
accessToken.setRefreshToken(refreshToken);
expiry = now.plusSeconds(config.getAccessTokenExpiry());
} else {
expiry = now.plusSeconds(config.getAccessTokenLongExpiry());
}
OAuth2Client client = clientDao.retrieveClientById(clientId);
accessToken.setCreatedDate(now);
accessToken.setExpiryDate(expiry);
accessToken.setToken(token);
accessToken.setScopes(scopes);
accessToken.setUserId(userId);
accessToken.setClient(client);
accessToken.setUserAuthenticationTime(authenticationTime);
entityManager.persist(accessToken);
}
use of de.ids_mannheim.korap.oauth2.entity.AccessToken in project Kustvakt by KorAP.
the class OltuTokenService method revokeAllClientTokensViaSuperClient.
public void revokeAllClientTokensViaSuperClient(String username, OAuth2RevokeAllTokenSuperRequest revokeTokenRequest) throws KustvaktException {
String superClientId = revokeTokenRequest.getSuperClientId();
String superClientSecret = revokeTokenRequest.getSuperClientSecret();
OAuth2Client superClient = clientService.authenticateClient(superClientId, superClientSecret);
if (!superClient.isSuper()) {
throw new KustvaktException(StatusCodes.CLIENT_AUTHENTICATION_FAILED);
}
String clientId = revokeTokenRequest.getClientId();
OAuth2Client client = clientService.retrieveClient(clientId);
if (clientService.isPublicClient(client)) {
List<AccessToken> accessTokens = tokenDao.retrieveAccessTokenByClientId(clientId, username);
for (AccessToken t : accessTokens) {
revokeAccessToken(t);
}
} else {
List<RefreshToken> refreshTokens = refreshDao.retrieveRefreshTokenByClientId(clientId, username);
for (RefreshToken r : refreshTokens) {
revokeRefreshToken(r);
}
}
}
use of de.ids_mannheim.korap.oauth2.entity.AccessToken in project Kustvakt by KorAP.
the class OltuTokenService method listUserAccessToken.
public List<OAuth2TokenDto> listUserAccessToken(String username, String superClientId, String superClientSecret, String clientId) throws KustvaktException {
OAuth2Client superClient = clientService.authenticateClient(superClientId, superClientSecret);
if (!superClient.isSuper()) {
throw new KustvaktException(StatusCodes.CLIENT_AUTHORIZATION_FAILED, "Only super client is allowed.", OAuth2Error.UNAUTHORIZED_CLIENT);
}
List<AccessToken> tokens = tokenDao.retrieveAccessTokenByUser(username, clientId);
List<OAuth2TokenDto> dtoList = new ArrayList<>(tokens.size());
for (AccessToken t : tokens) {
OAuth2Client tokenClient = t.getClient();
if (tokenClient.getId().equals(superClient.getId())) {
continue;
}
OAuth2TokenDto dto = new OAuth2TokenDto();
dto.setClientId(tokenClient.getId());
dto.setClientName(tokenClient.getName());
dto.setClientUrl(tokenClient.getUrl());
dto.setClientDescription(tokenClient.getDescription());
DateTimeFormatter f = DateTimeFormatter.ISO_DATE_TIME;
dto.setCreatedDate(t.getCreatedDate().format(f));
long difference = ChronoUnit.SECONDS.between(ZonedDateTime.now(), t.getExpiryDate());
dto.setExpiresIn(difference);
dto.setUserAuthenticationTime(t.getUserAuthenticationTime().format(f));
dto.setToken(t.getToken());
Set<AccessScope> accessScopes = t.getScopes();
Set<String> scopes = new HashSet<>(accessScopes.size());
for (AccessScope s : accessScopes) {
scopes.add(s.getId().toString());
}
dto.setScope(scopes);
dtoList.add(dto);
}
return dtoList;
}
Aggregations