2424import eu .openanalytics .containerproxy .auth .impl .msgraph .MicrosoftGraphGroupFetcher ;
2525import eu .openanalytics .containerproxy .auth .impl .oidc .AccessTokenDecoder ;
2626import eu .openanalytics .containerproxy .auth .impl .oidc .OpenIdReAuthorizeFilter ;
27+ import eu .openanalytics .containerproxy .model .runtime .Proxy ;
2728import eu .openanalytics .containerproxy .spec .expression .SpecExpressionContext ;
2829import eu .openanalytics .containerproxy .spec .expression .SpecExpressionResolver ;
2930import eu .openanalytics .containerproxy .util .ContextPathHelper ;
31+ import io .undertow .util .HeaderMap ;
32+ import io .undertow .util .HttpString ;
3033import net .minidev .json .JSONArray ;
3134import net .minidev .json .parser .JSONParser ;
3235import net .minidev .json .parser .ParseException ;
4144import org .springframework .security .core .GrantedAuthority ;
4245import org .springframework .security .core .authority .SimpleGrantedAuthority ;
4346import org .springframework .security .core .authority .mapping .GrantedAuthoritiesMapper ;
47+ import org .springframework .security .core .context .SecurityContextHolder ;
4448import org .springframework .security .oauth2 .client .OAuth2AuthorizedClient ;
4549import org .springframework .security .oauth2 .client .OAuth2AuthorizedClientService ;
4650import org .springframework .security .oauth2 .client .oidc .userinfo .OidcUserRequest ;
5054import org .springframework .security .oauth2 .client .web .OAuth2AuthorizationRequestCustomizers ;
5155import org .springframework .security .oauth2 .client .web .OAuth2AuthorizationRequestRedirectFilter ;
5256import org .springframework .security .oauth2 .client .web .OAuth2AuthorizationRequestResolver ;
57+ import org .springframework .security .oauth2 .core .OAuth2AccessToken ;
5358import org .springframework .security .oauth2 .core .OAuth2AuthenticationException ;
5459import org .springframework .security .oauth2 .core .OAuth2Error ;
5560import org .springframework .security .oauth2 .core .OAuth2ErrorCodes ;
61+ import org .springframework .security .oauth2 .core .OAuth2RefreshToken ;
5662import org .springframework .security .oauth2 .core .oidc .OidcIdToken ;
5763import org .springframework .security .oauth2 .core .oidc .OidcUserInfo ;
5864import org .springframework .security .oauth2 .core .oidc .StandardClaimAccessor ;
@@ -83,11 +89,22 @@ public class OpenIDAuthenticationBackend implements IAuthenticationBackend {
8389 public static final String NAME = "openid" ;
8490
8591 private static final String ENV_TOKEN_NAME = "SHINYPROXY_OIDC_ACCESS_TOKEN" ;
92+ private static final String PROP_SEND_ACCESS_TOKEN_HEADER = "proxy.openid.add-access-token-header" ;
93+ private static final String PROP_SEND_REFRESH_TOKEN_HEADER = "proxy.openid.add-refresh-token-header" ;
94+ private static final String PROP_SEND_ID_TOKEN_HEADER = "proxy.openid.add-id-token-header" ;
95+ private static final HttpString HEADER_ACCESS_TOKEN_NAME = new HttpString ("X-SP-OpenId-Access-Token" );
96+ private static final HttpString HEADER_REFRESH_TOKEN_NAME = new HttpString ("X-SP-OpenId-Refresh-Token" );
97+ private static final HttpString HEADER_ID_TOKEN_NAME = new HttpString ("X-SP-OpenId-Id-Token" );
98+
8699 private static OAuth2AuthorizedClientService oAuth2AuthorizedClientService ;
87100 private static AccessTokenDecoder accessTokenDecoder ;
88101 private static final Logger log = LogManager .getLogger (OpenIDAuthenticationBackend .class );
89- @ Inject
90- private Environment environment ;
102+
103+ private static Boolean sendAccessTokenHeader = false ;
104+ private static Boolean sendRefreshTokenHeader = false ;
105+ private static Boolean sendIdTokenHeader = false ;
106+ private static Environment environment ;
107+
91108 @ Inject
92109 private ClientRegistrationRepository clientRegistrationRepo ;
93110 @ Inject
@@ -180,7 +197,7 @@ public Set<GrantedAuthority> parseClaims(StandardClaimAccessor standardClaimAcce
180197 return mappedAuthorities ;
181198 }
182199
183- private static OAuth2AuthorizedClient refreshClient (String principalName ) {
200+ public static OAuth2AuthorizedClient refreshClient (String principalName ) {
184201 return oAuth2AuthorizedClientService .loadAuthorizedClient (REG_ID , principalName );
185202 }
186203
@@ -194,6 +211,14 @@ public void setOAuth2AuthorizedClientService(OAuth2AuthorizedClientService oAuth
194211 OpenIDAuthenticationBackend .oAuth2AuthorizedClientService = oAuth2AuthorizedClientService ;
195212 }
196213
214+ @ Autowired
215+ public void setEnvironment (Environment environment ) {
216+ OpenIDAuthenticationBackend .environment = environment ;
217+ OpenIDAuthenticationBackend .sendAccessTokenHeader = environment .getProperty (PROP_SEND_ACCESS_TOKEN_HEADER , Boolean .class , false );
218+ OpenIDAuthenticationBackend .sendRefreshTokenHeader = environment .getProperty (PROP_SEND_REFRESH_TOKEN_HEADER , Boolean .class , false );
219+ OpenIDAuthenticationBackend .sendIdTokenHeader = environment .getProperty (PROP_SEND_ID_TOKEN_HEADER , Boolean .class , false );
220+ }
221+
197222 @ Override
198223 public String getName () {
199224 return NAME ;
@@ -350,6 +375,38 @@ public OidcUser loadUser(OidcUserRequest userRequest) throws OAuth2Authenticatio
350375 };
351376 }
352377
378+ public static HeaderMap addHeaders (Proxy proxy ) {
379+ HeaderMap result = new HeaderMap ();
380+ if (sendAccessTokenHeader || sendRefreshTokenHeader ) {
381+ OAuth2AuthorizedClient client = refreshClient (proxy .getUserId ());
382+ if (client != null ) {
383+ if (sendAccessTokenHeader ) {
384+ OAuth2AccessToken accessToken = client .getAccessToken ();
385+ if (accessToken != null ) {
386+ result .put (HEADER_ACCESS_TOKEN_NAME , accessToken .getTokenValue ());
387+ }
388+ }
389+ if (sendRefreshTokenHeader ) {
390+ OAuth2RefreshToken refreshToken = client .getRefreshToken ();
391+ if (refreshToken != null ) {
392+ result .put (HEADER_REFRESH_TOKEN_NAME , refreshToken .getTokenValue ());
393+ }
394+ }
395+ }
396+ }
397+ if (sendIdTokenHeader ) {
398+ Authentication auth = SecurityContextHolder .getContext ().getAuthentication ();
399+ if (auth .getPrincipal () instanceof CustomNameOidcUser ) {
400+ OidcIdToken idToken = ((CustomNameOidcUser ) auth .getPrincipal ()).getIdToken ();
401+ if (idToken != null ) {
402+ result .put (HEADER_ID_TOKEN_NAME , idToken .getTokenValue ());
403+ }
404+ }
405+ }
406+
407+ return result ;
408+ }
409+
353410 public static class CustomNameOidcUser extends DefaultOidcUser {
354411
355412 private static final long serialVersionUID = 7563253562760236634L ;
0 commit comments