11
11
import org .springframework .security .oauth2 .jwt .JwtClaimNames ;
12
12
import org .springframework .security .oauth2 .server .resource .authentication .JwtAuthenticationToken ;
13
13
import org .springframework .stereotype .Service ;
14
+ import reactor .core .publisher .Mono ;
14
15
15
16
import java .util .Optional ;
16
17
@@ -25,25 +26,28 @@ public class ProfilService {
25
26
private final PersonOrganisasjonTilgangConsumer organisasjonTilgangConsumer ;
26
27
private final GetUserInfo getUserInfo ;
27
28
28
- public Profil getProfile () {
29
+ public Mono <Profil > getProfile () {
30
+
29
31
if (isTokenX ()) {
30
32
return getUserInfo .call ()
31
- .map (userInfo -> new Profil (
32
- userInfo .brukernavn (),
33
- UKJENT ,
34
- UKJENT ,
35
- UKJENT ,
36
- userInfo .organisasjonsnummer (),
37
- BANK_ID )
38
- )
39
- .orElse (new Profil (
40
- BANK_ID ,
41
- UKJENT ,
42
- UKJENT ,
43
- UKJENT ,
44
- UKJENT ,
45
- BANK_ID
46
- ));
33
+ .map (userInfo -> organisasjonTilgangConsumer
34
+ .getOrganisasjon (getIdent (), userInfo .organisasjonsnummer ())
35
+ .map (organisasjon -> new Profil (
36
+ userInfo .brukernavn (),
37
+ UKJENT ,
38
+ UKJENT ,
39
+ organisasjon .getNavn (),
40
+ userInfo .organisasjonsnummer (),
41
+ BANK_ID )
42
+ ))
43
+ .orElse (Mono .just (new Profil (
44
+ BANK_ID ,
45
+ UKJENT ,
46
+ UKJENT ,
47
+ UKJENT ,
48
+ UKJENT ,
49
+ BANK_ID
50
+ )));
47
51
}
48
52
return azureAdProfileConsumer .getProfil ();
49
53
}
@@ -52,18 +56,29 @@ public Optional<byte[]> getImage() {
52
56
return isTokenX () ? Optional .empty () : azureAdProfileConsumer .getProfilImage ();
53
57
}
54
58
55
- private JwtAuthenticationToken getJwtAuthenticationToken () {
59
+ private Optional <JwtAuthenticationToken > getJwtAuthenticationToken () {
60
+
56
61
return Optional .ofNullable (SecurityContextHolder .getContext ().getAuthentication ())
57
62
.filter (JwtAuthenticationToken .class ::isInstance )
58
- .map (JwtAuthenticationToken .class ::cast )
59
- .orElseThrow ();
63
+ .map (JwtAuthenticationToken .class ::cast );
60
64
}
61
65
62
66
private boolean isTokenX () {
63
67
64
68
return getJwtAuthenticationToken ()
65
- .getTokenAttributes ()
66
- .get (JwtClaimNames .ISS )
67
- .equals (tokenXResourceServerProperties .getIssuerUri ());
69
+ .map (token -> token
70
+ .getTokenAttributes ()
71
+ .get (JwtClaimNames .ISS )
72
+ .equals (tokenXResourceServerProperties .getIssuerUri ()))
73
+ .orElseThrow ();
74
+ }
75
+
76
+ private String getIdent () {
77
+
78
+ return getJwtAuthenticationToken ()
79
+ .map (JwtAuthenticationToken ::getTokenAttributes )
80
+ .map (attribs -> attribs .get ("pid" ))
81
+ .map (ident -> (String ) ident )
82
+ .orElseThrow ();
68
83
}
69
84
}
0 commit comments