1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.oauth2.business;
35
36 import java.io.IOException;
37 import java.util.Objects;
38 import java.util.Set;
39
40 import com.fasterxml.jackson.databind.DeserializationFeature;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42
43 import fr.paris.lutece.plugins.oauth2.service.CachingHttpAccessService;
44 import fr.paris.lutece.portal.service.util.AppException;
45 import fr.paris.lutece.util.httpaccess.HttpAccess;
46 import fr.paris.lutece.util.httpaccess.HttpAccessException;
47 import fr.paris.lutece.util.httpaccess.HttpAccessService;
48 import fr.paris.lutece.util.httpaccess.PropertiesHttpClientConfiguration;
49
50
51
52
53
54
55 public class OIDCAuthServerConf extends AuthServerConf
56 {
57 private static final long serialVersionUID = 3459341547945895738L;
58 private static final String WELLKNOWN_PATH = ".well-known/openid-configuration";
59
60 private final HttpAccess _httpAccess;
61 private final ObjectMapper _mapper;
62
63 public OIDCAuthServerConf( )
64 {
65 HttpAccessService accessService = new CachingHttpAccessService( new PropertiesHttpClientConfiguration( ) );
66 this._httpAccess = new HttpAccess( accessService );
67 this._mapper = new ObjectMapper( );
68 this._mapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
69 }
70
71 @Override
72 public boolean isEnableJwtParser( )
73 {
74 return true;
75 }
76
77 @Override
78 public String getAuthorizationEndpointUri( )
79 {
80 if ( super.getAuthorizationEndpointUri( ) != null )
81 {
82 return super.getAuthorizationEndpointUri( );
83 }
84 return getOpenidConfiguration( ).getAuthorizationEndpoint( );
85 }
86
87 @Override
88 public String getTokenEndpointUri( )
89 {
90 if ( super.getTokenEndpointUri( ) != null )
91 {
92 return super.getTokenEndpointUri( );
93 }
94 return getOpenidConfiguration( ).getTokenEndpoint( );
95 }
96
97 @Override
98 public Set<String> getIDTokenSignatureAlgorithmNames( )
99 {
100 if ( super.getIDTokenSignatureAlgorithmNames( ) != null )
101 {
102 return super.getIDTokenSignatureAlgorithmNames( );
103 }
104 return Set.of( getOpenidConfiguration( ).getIDTokenSigningAlgValuesSupported( ) );
105 }
106
107 @Override
108 public String getJwksEndpointUri( )
109 {
110 if ( super.getJwksEndpointUri( ) != null )
111 {
112 return super.getJwksEndpointUri( );
113 }
114 return getOpenidConfiguration( ).getJwksURI( );
115 }
116
117 @Override
118 public String getLogoutEndpointUri( )
119 {
120 if ( super.getLogoutEndpointUri( ) != null )
121 {
122 return super.getLogoutEndpointUri( );
123 }
124 return getOpenidConfiguration( ).getEndSessionEndpoint( );
125 }
126
127 private OpenIDConfiguration getOpenidConfiguration( )
128 {
129 String issuer = getIssuer( );
130 Objects.requireNonNull( issuer, "issuer must not be null" );
131 if ( !issuer.startsWith( "https" ) )
132 {
133 throw new UnsupportedOperationException( "The issuer must start with https, but is " + issuer );
134 }
135 String strConfURL;
136 if ( issuer.endsWith( "/" ) )
137 {
138 strConfURL = issuer + WELLKNOWN_PATH;
139 }
140 else
141 {
142 strConfURL = issuer + "/" + WELLKNOWN_PATH;
143 }
144 try
145 {
146 String strConfiguration = _httpAccess.doGet( strConfURL );
147 OpenIDConfiguration res = this._mapper.readValue( strConfiguration, OpenIDConfiguration.class );
148 res.validate( issuer );
149 return res;
150 }
151 catch( HttpAccessException | IOException e )
152 {
153 throw new AppException( e.getMessage( ), e );
154 }
155 }
156 }