| margaretha | 07402f4 | 2018-05-07 19:07:45 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.authentication; |
| 2 | |
| margaretha | 07402f4 | 2018-05-07 19:07:45 +0200 | [diff] [blame] | 3 | import java.util.Map; |
| 4 | |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.stereotype.Component; |
| 7 | |
| 8 | import de.ids_mannheim.korap.config.Attributes; |
| margaretha | 07402f4 | 2018-05-07 19:07:45 +0200 | [diff] [blame] | 9 | import de.ids_mannheim.korap.constant.TokenType; |
| 10 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 11 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 12 | import de.ids_mannheim.korap.interfaces.AuthenticationIface; |
| 13 | import de.ids_mannheim.korap.oauth2.dao.AccessTokenDao; |
| 14 | import de.ids_mannheim.korap.oauth2.entity.AccessToken; |
| 15 | import de.ids_mannheim.korap.oauth2.service.OAuth2ScopeService; |
| 16 | import de.ids_mannheim.korap.security.context.TokenContext; |
| 17 | import de.ids_mannheim.korap.user.User; |
| 18 | |
| 19 | @Component |
| 20 | public class OAuth2Authentication implements AuthenticationIface { |
| 21 | |
| 22 | @Autowired |
| 23 | private AccessTokenDao accessDao; |
| 24 | @Autowired |
| 25 | private OAuth2ScopeService scopeService; |
| margaretha | 07402f4 | 2018-05-07 19:07:45 +0200 | [diff] [blame] | 26 | |
| 27 | @Override |
| 28 | public TokenContext getTokenContext (String authToken) |
| 29 | throws KustvaktException { |
| 30 | |
| 31 | AccessToken accessToken = accessDao.retrieveAccessToken(authToken); |
| 32 | if (accessToken.isRevoked()) { |
| margaretha | dc51507 | 2018-08-03 17:01:19 +0200 | [diff] [blame] | 33 | throw new KustvaktException(StatusCodes.INVALID_ACCESS_TOKEN, |
| 34 | "Access token has been revoked"); |
| margaretha | 07402f4 | 2018-05-07 19:07:45 +0200 | [diff] [blame] | 35 | } |
| margaretha | cf306d3 | 2018-05-30 19:45:35 +0200 | [diff] [blame] | 36 | |
| margaretha | 07402f4 | 2018-05-07 19:07:45 +0200 | [diff] [blame] | 37 | String scopes = scopeService |
| 38 | .convertAccessScopesToString(accessToken.getScopes()); |
| 39 | |
| 40 | TokenContext c = new TokenContext(); |
| 41 | c.setUsername(accessToken.getUserId()); |
| margaretha | 6ad08b4 | 2018-08-22 18:33:54 +0200 | [diff] [blame^] | 42 | c.setExpirationTime(accessToken.getExpiryDate().toInstant().toEpochMilli()); |
| margaretha | 07402f4 | 2018-05-07 19:07:45 +0200 | [diff] [blame] | 43 | c.setToken(authToken); |
| 44 | c.setTokenType(TokenType.BEARER); |
| margaretha | 20f3123 | 2018-07-09 17:49:39 +0200 | [diff] [blame] | 45 | c.addContextParameter(Attributes.SCOPE, scopes); |
| margaretha | a2ce63d | 2018-06-28 10:11:43 +0200 | [diff] [blame] | 46 | c.setAuthenticationTime(accessToken.getUserAuthenticationTime()); |
| margaretha | 07402f4 | 2018-05-07 19:07:45 +0200 | [diff] [blame] | 47 | 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 | } |