blob: d203b514ab205227716568aa87ff591e3f25e6d2 [file] [log] [blame]
margaretha07402f42018-05-07 19:07:45 +02001package de.ids_mannheim.korap.authentication;
2
margaretha07402f42018-05-07 19:07:45 +02003import java.util.Map;
4
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.stereotype.Component;
7
8import de.ids_mannheim.korap.config.Attributes;
margaretha07402f42018-05-07 19:07:45 +02009import de.ids_mannheim.korap.constant.TokenType;
10import de.ids_mannheim.korap.exceptions.KustvaktException;
11import de.ids_mannheim.korap.exceptions.StatusCodes;
12import de.ids_mannheim.korap.interfaces.AuthenticationIface;
13import de.ids_mannheim.korap.oauth2.dao.AccessTokenDao;
14import de.ids_mannheim.korap.oauth2.entity.AccessToken;
15import de.ids_mannheim.korap.oauth2.service.OAuth2ScopeService;
16import de.ids_mannheim.korap.security.context.TokenContext;
17import de.ids_mannheim.korap.user.User;
18
19@Component
20public class OAuth2Authentication implements AuthenticationIface {
21
22 @Autowired
23 private AccessTokenDao accessDao;
24 @Autowired
25 private OAuth2ScopeService scopeService;
margaretha07402f42018-05-07 19:07:45 +020026
27 @Override
28 public TokenContext getTokenContext (String authToken)
29 throws KustvaktException {
30
31 AccessToken accessToken = accessDao.retrieveAccessToken(authToken);
32 if (accessToken.isRevoked()) {
margarethadc515072018-08-03 17:01:19 +020033 throw new KustvaktException(StatusCodes.INVALID_ACCESS_TOKEN,
34 "Access token has been revoked");
margaretha07402f42018-05-07 19:07:45 +020035 }
margarethacf306d32018-05-30 19:45:35 +020036
margaretha07402f42018-05-07 19:07:45 +020037 String scopes = scopeService
38 .convertAccessScopesToString(accessToken.getScopes());
39
40 TokenContext c = new TokenContext();
41 c.setUsername(accessToken.getUserId());
margaretha6ad08b42018-08-22 18:33:54 +020042 c.setExpirationTime(accessToken.getExpiryDate().toInstant().toEpochMilli());
margaretha07402f42018-05-07 19:07:45 +020043 c.setToken(authToken);
44 c.setTokenType(TokenType.BEARER);
margaretha20f31232018-07-09 17:49:39 +020045 c.addContextParameter(Attributes.SCOPE, scopes);
margarethaa2ce63d2018-06-28 10:11:43 +020046 c.setAuthenticationTime(accessToken.getUserAuthenticationTime());
margaretha07402f42018-05-07 19:07:45 +020047 return c;
48 }
49
50 @Override
51 public TokenContext createTokenContext (User user, Map<String, Object> attr)
52 throws KustvaktException {
53 // TODO Auto-generated method stub
54 return null;
55 }
56
57 @Override
58 public void removeUserSession (String token) throws KustvaktException {
59 // TODO Auto-generated method stub
60
61 }
62
63 @Override
64 public TokenContext refresh (TokenContext context)
65 throws KustvaktException {
66 // TODO Auto-generated method stub
67 return null;
68 }
69
70 @Override
71 public TokenType getTokenType () {
72 return TokenType.BEARER;
73 }
74
75}