11package com .continiousdisappointment .user .service ;
22
33import org .springframework .http .*;
4- import org .springframework .security .core .Authentication ;
5- import org .springframework .security .core .context .SecurityContextHolder ;
6- import org .springframework .security .oauth2 .core .OAuth2AccessToken ;
74import org .springframework .stereotype .Service ;
85import org .springframework .web .client .RestTemplate ;
96
@@ -14,14 +11,13 @@ public class UserService {
1411
1512 private final RestTemplate restTemplate = new RestTemplate ();
1613
17- public User getUserInfo () {
14+ public User getUserInfo (String authorization ) {
1815 String url = "https://gitlab.lrz.de/api/v4/user" ;
19- String accessToken = getBearerToken ();
20- if (accessToken == null ) {
16+ if (authorization == null ) {
2117 throw new IllegalStateException ("No access token found in security context" );
2218 }
2319 HttpHeaders headers = new HttpHeaders ();
24- headers .setBearerAuth ( accessToken ); // Sets Authorization: Bearer <token>
20+ headers .set ( HttpHeaders . AUTHORIZATION , authorization ); // Sets Authorization: Bearer <token>
2521
2622 HttpEntity <Void > requestEntity = new HttpEntity <>(headers );
2723
@@ -32,17 +28,4 @@ public User getUserInfo() {
3228 User .class );
3329 return response .getBody ();
3430 }
35-
36- private String getBearerToken () {
37- Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
38- if (authentication == null ) {
39- return null ;
40- }
41- OAuth2AccessToken credentials = (OAuth2AccessToken ) authentication .getCredentials ();
42- if (credentials != null ) {
43- return credentials .getTokenValue ();
44- }
45- return null ;
46- }
47-
4831}
0 commit comments