1
1
package life .qbic .identity .application .token ;
2
2
3
+ import static java .util .Objects .requireNonNull ;
4
+
3
5
import java .time .Duration ;
4
6
import java .util .Collection ;
5
7
import life .qbic .identity .api .PersonalAccessToken ;
6
8
import life .qbic .identity .api .PersonalAccessTokenService ;
7
9
import life .qbic .identity .api .RawToken ;
8
10
import life .qbic .identity .api .UnknownUserIdException ;
9
11
import life .qbic .identity .api .UserInformationService ;
12
+ import life .qbic .identity .domain .model .token .TokenEncoder ;
10
13
import life .qbic .identity .domain .model .token .TokenGenerator ;
11
14
import life .qbic .identity .domain .model .token .TokenRepository ;
12
15
import org .springframework .stereotype .Service ;
22
25
public class PersonalAccessTokenServiceImpl implements PersonalAccessTokenService {
23
26
24
27
private final UserInformationService userInformationService ;
25
-
26
28
private final TokenRepository tokenRepository ;
29
+ private final TokenEncoder tokenEncoder ;
27
30
28
31
public PersonalAccessTokenServiceImpl (UserInformationService basicUserInformationService ,
29
- TokenRepository tokenRepository ) {
30
- this .userInformationService = basicUserInformationService ;
31
- this .tokenRepository = tokenRepository ;
32
+ TokenRepository tokenRepository ,
33
+ TokenEncoder tokenEncoder ) {
34
+ this .userInformationService = requireNonNull (basicUserInformationService ,
35
+ "basicUserInformationService must not be null" );
36
+ this .tokenRepository = requireNonNull (tokenRepository , "tokenRepository must not be null" );
37
+ this .tokenEncoder = requireNonNull (tokenEncoder , "tokenEncoder must not be null" );
32
38
}
33
39
34
40
private static PersonalAccessToken convert (
@@ -49,16 +55,16 @@ public RawToken create(String userId, String description, Duration duration)
49
55
}
50
56
51
57
private RawToken processTokenRequest (String userId , String description , Duration duration ) {
52
- TokenGenerator tokenGenerator = new TokenGenerator ();
53
- String rawToken = tokenGenerator . token ( );
54
- var token = life .qbic .identity .domain .model .token .PersonalAccessToken . create (userId ,
55
- description , duration , rawToken );
58
+ String rawToken = TokenGenerator . token ();
59
+ String encodedToken = tokenEncoder . encode ( rawToken . toCharArray () );
60
+ var token = new life .qbic .identity .domain .model .token .PersonalAccessToken (userId , description ,
61
+ duration , encodedToken );
56
62
tokenRepository .save (token );
57
63
return new RawToken (rawToken );
58
64
}
59
65
60
66
@ Override
61
- public Collection <PersonalAccessToken > find (String userId ) {
67
+ public Collection <PersonalAccessToken > findAll (String userId ) {
62
68
return tokenRepository .findAllByUserId (userId ).stream ()
63
69
.map (PersonalAccessTokenServiceImpl ::convert ).toList ();
64
70
}
0 commit comments