2
2
3
3
import org .mskcc .cbio .oncokb .domain .Authority ;
4
4
import org .mskcc .cbio .oncokb .domain .Token ;
5
+ import org .mskcc .cbio .oncokb .domain .TokenKey ;
5
6
import org .mskcc .cbio .oncokb .domain .User ;
7
+ import org .mskcc .cbio .oncokb .domain .enumeration .TokenType ;
6
8
import org .mskcc .cbio .oncokb .repository .UserRepository ;
7
9
import org .mskcc .cbio .oncokb .security .AuthoritiesConstants ;
8
10
import org .mskcc .cbio .oncokb .security .SecurityUtils ;
@@ -50,11 +52,11 @@ public List<Token> getUserTokens(User userLogin) {
50
52
return tokenService .findByUser (userLogin );
51
53
}
52
54
53
- private Token getNewToken (Set <Authority > authorities , Optional <Instant > definedExpirationTime ) {
54
- return getNewToken (authorities , definedExpirationTime , Optional .of (true ));
55
+ private Token getNewToken (Set <Authority > authorities , TokenType tokenType , Optional <Instant > definedExpirationTime ) {
56
+ return getNewToken (authorities , tokenType , definedExpirationTime , Optional .of (true ));
55
57
}
56
58
57
- private Token getNewToken (Set <Authority > authorities , Optional <Instant > definedExpirationTime , Optional <Boolean > isRenewable ) {
59
+ private Token getNewToken (Set <Authority > authorities , TokenType tokenType , Optional <Instant > definedExpirationTime , Optional <Boolean > isRenewable ) {
58
60
Token token = new Token ();
59
61
Instant currentTime = Instant .now ();
60
62
token .setCreation (currentTime );
@@ -73,20 +75,20 @@ private Token getNewToken(Set<Authority> authorities, Optional<Instant> definedE
73
75
Instant .now ().plusSeconds (EXPIRATION_TIME_PUBLIC_WEBSITE_IN_SECONDS ) : currentTime .plusSeconds (EXPIRATION_TIME_IN_SECONDS );
74
76
token .setExpiration (expirationTime );
75
77
}
76
- token .setToken (UUID . randomUUID ());
78
+ token .setToken (TokenKey . generate ( tokenType ). getFullToken ());
77
79
return token ;
78
80
}
79
81
80
82
public Token createTokenForCurrentUserLogin (Optional <Instant > definedExpirationTime , Optional <Boolean > isRenewable ) {
81
83
Optional <User > userOptional = userRepository .findOneWithAuthoritiesByLogin (SecurityUtils .getCurrentUserLogin ().get ());
82
84
if (userOptional .isPresent ()) {
83
- return createToken (userOptional .get (), definedExpirationTime , isRenewable , Optional .empty ());
85
+ return createToken (userOptional .get (), TokenType . USER , definedExpirationTime , isRenewable , Optional .empty ());
84
86
}
85
87
return null ;
86
88
}
87
89
88
- public Token createToken (User user , Optional <Instant > definedExpirationTime , Optional <Boolean > isRenewable , Optional <String > name ) {
89
- Token token = getNewToken (user .getAuthorities (), definedExpirationTime , isRenewable );
90
+ public Token createToken (User user , TokenType tokenType , Optional <Instant > definedExpirationTime , Optional <Boolean > isRenewable , Optional <String > name ) {
91
+ Token token = getNewToken (user .getAuthorities (), tokenType , definedExpirationTime , isRenewable );
90
92
token .setUser (user );
91
93
if (name .isPresent ()) {
92
94
token .setName (name .get ());
@@ -95,29 +97,29 @@ public Token createToken(User user, Optional<Instant> definedExpirationTime, Opt
95
97
return token ;
96
98
}
97
99
98
- public void createToken (Token token , Optional <String > name ){
99
- Token newToken = createToken (token .getUser (), Optional .of (token .getExpiration ()), Optional .of (token .isRenewable ()), name );
100
+ public void createToken (Token token , TokenType tokenType , Optional <String > name ){
101
+ Token newToken = createToken (token .getUser (), tokenType , Optional .of (token .getExpiration ()), Optional .of (token .isRenewable ()), name );
100
102
newToken .setCreation (token .getCreation ());
101
103
newToken .setCurrentUsage (token .getCurrentUsage ());
102
104
newToken .setUsageLimit (token .getUsageLimit ());
103
105
tokenService .save (newToken );
104
106
}
105
107
106
108
// This method is used in the frontend thymeleaf parsing
107
- public UUID getPubWebToken () {
109
+ public String getPubWebToken () {
108
110
Optional <User > user = userRepository .findOneWithAuthoritiesByLogin (PUBLIC_WEBSITE_LOGIN );
109
111
if (user .isPresent ()) {
110
112
Token userToken = new Token ();
111
113
Optional <Token > tokenOptional = tokenService .findPublicWebsiteToken ();
112
114
if (!tokenOptional .isPresent ()) {
113
- Token newToken = getNewToken (user .get ().getAuthorities (), Optional .empty ());
115
+ Token newToken = getNewToken (user .get ().getAuthorities (), TokenType . USER , Optional .empty ());
114
116
newToken .setUser (user .get ());
115
117
userToken = tokenService .save (newToken );
116
118
} else {
117
119
userToken = tokenOptional .get ();
118
120
if (userToken .getExpiration ().isBefore (Instant .now ())) {
119
121
// I want to update the token associated with public website once it's expired
120
- Token newToken = getNewToken (user .get ().getAuthorities (), Optional .empty (), Optional .empty ());
122
+ Token newToken = getNewToken (user .get ().getAuthorities (), TokenType . USER , Optional .empty (), Optional .empty ());
121
123
userToken .setToken (newToken .getToken ());
122
124
userToken .setCreation (newToken .getCreation ());
123
125
userToken .setExpiration (newToken .getExpiration ());
@@ -131,7 +133,7 @@ public UUID getPubWebToken() {
131
133
return null ;
132
134
}
133
135
134
- public Authentication getAuthentication (UUID token ) {
136
+ public Authentication getAuthentication (String token ) {
135
137
Optional <Token > tokenOptional = tokenService .findByToken (token );
136
138
137
139
Optional <User > user = userRepository .findOneWithAuthoritiesByLogin (tokenOptional .get ().getUser ().getLogin ());
@@ -143,7 +145,7 @@ public Authentication getAuthentication(UUID token) {
143
145
return new UsernamePasswordAuthenticationToken (user .get ().getLogin (), token , authorities );
144
146
}
145
147
146
- public boolean validateToken (UUID tokenValue ) {
148
+ public boolean validateToken (String tokenValue ) {
147
149
try {
148
150
Optional <Token > token = tokenService .findByToken (tokenValue );
149
151
if (token .isPresent () && token .get ().getExpiration ().isAfter (Instant .now ())) {
0 commit comments