4747/**
4848 * Run with "mvn spring-boot:run".
4949 * <p>
50- * Provides OIDC metadata. Seet the spec at https://openid.net/specs/openid-connect-discovery-1_0.html
50+ * Provides OIDC metadata. See the spec at https://openid.net/specs/openid-connect-discovery-1_0.html
5151 */
5252@ RestController
5353public class OidcController {
@@ -59,7 +59,7 @@ public class OidcController {
5959 public static final String TOKEN_ENDPOINT = "/token" ;
6060 public static final String USERINFO_ENDPOINT = "/userinfo" ;
6161 public static final String JWKS_ENDPOINT = "/jwks" ;
62- public static final String INTROSPECTION_ENDPOINT = "/introspection " ;
62+ public static final String INTROSPECTION_ENDPOINT = "/introspect " ;
6363
6464 private JWSSigner signer ;
6565 private JWKSet publicJWKSet ;
@@ -84,7 +84,7 @@ public void init() throws IOException, ParseException, JOSEException {
8484 @ RequestMapping (value = METADATA_ENDPOINT , method = RequestMethod .GET , produces = MediaType .APPLICATION_JSON_VALUE )
8585 @ CrossOrigin
8686 public ResponseEntity <?> metadata (UriComponentsBuilder uriBuilder , HttpServletRequest req ) {
87- log .info (METADATA_ENDPOINT + " from {}" , req .getRemoteHost ());
87+ log .info ("called " + METADATA_ENDPOINT + " from {}" , req .getRemoteHost ());
8888 String urlPrefix = uriBuilder .replacePath (null ).build ().encode ().toUriString ();
8989 Map <String , Object > m = new LinkedHashMap <>();
9090 // https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata
@@ -104,15 +104,15 @@ public ResponseEntity<?> metadata(UriComponentsBuilder uriBuilder, HttpServletRe
104104
105105 @ RequestMapping (value = JWKS_ENDPOINT , method = RequestMethod .GET , produces = MediaType .APPLICATION_JSON_VALUE )
106106 @ CrossOrigin
107- public ResponseEntity <String > jwks () {
108- log .info ("/jwks" );
107+ public ResponseEntity <String > jwks (HttpServletRequest req ) {
108+ log .info ("called " + JWKS_ENDPOINT + " from {}" , req . getRemoteHost () );
109109 return ResponseEntity .ok ().body (publicJWKSet .toString ());
110110 }
111111
112112 @ RequestMapping (value = USERINFO_ENDPOINT , method = RequestMethod .GET , produces = MediaType .APPLICATION_JSON_VALUE )
113113 @ CrossOrigin (allowedHeaders = {"Authorization" , "Content-Type" })
114- public ResponseEntity <?> userinfo (@ RequestHeader ("Authorization" ) String auth ) {
115- log .info ("/userinfo" );
114+ public ResponseEntity <?> userinfo (@ RequestHeader ("Authorization" ) String auth , HttpServletRequest req ) {
115+ log .info ("called " + USERINFO_ENDPOINT + " from {}" , req . getRemoteHost () );
116116 if (!auth .startsWith ("Bearer " )) {
117117 return ResponseEntity .status (HttpStatus .UNAUTHORIZED ).body ("No token" );
118118 }
@@ -134,17 +134,24 @@ public ResponseEntity<?> userinfo(@RequestHeader("Authorization") String auth) {
134134
135135 @ RequestMapping (value = INTROSPECTION_ENDPOINT , method = RequestMethod .POST , produces = MediaType .APPLICATION_JSON_VALUE )
136136 public ResponseEntity <?> introspection (@ RequestParam String token ,
137- @ RequestHeader ("Authorization" ) String auth ) {
138- log .info ("/introspection auth = {} token= {}" , auth , token );
137+ @ RequestHeader ("Authorization" ) String auth ,
138+ UriComponentsBuilder uriBuilder ,
139+ HttpServletRequest req ) {
140+ log .info ("called " + INTROSPECTION_ENDPOINT + " from {}" , req .getRemoteHost ());
139141 Map <String , Object > m = new LinkedHashMap <>();
140142 AccessTokenInfo accessTokenInfo = accessTokens .get (token );
141143 if ( accessTokenInfo == null ) {
144+ log .error ("token not found in memory: {}" , token );
142145 m .put ("active" , false );
143146 } else {
147+ String scopes = String .join (" " , accessTokenInfo .scopes );
148+ log .info ("token found, releasing scopes: {}" , scopes );
149+ m .put ("iss" , uriBuilder .replacePath (null ).build ().encode ().toUriString () + "/" );
144150 m .put ("active" , true );
145- m .put ("scope" , String . join ( " " , accessTokenInfo . scopes ) );
151+ m .put ("scope" , scopes );
146152 m .put ("username" , accessTokenInfo .user .getSub ());
147153 m .put ("sub" , accessTokenInfo .user .getSub ());
154+ m .put ("exp" , accessTokenInfo .expiration .toInstant ().toEpochMilli ());
148155 }
149156 return ResponseEntity .ok ().body (m );
150157 }
@@ -157,8 +164,10 @@ public ResponseEntity<?> authorize(@RequestParam String client_id,
157164 @ RequestParam String state ,
158165 @ RequestParam String nonce ,
159166 @ RequestHeader (name = "Authorization" , required = false ) String auth ,
160- UriComponentsBuilder uriBuilder ) throws JOSEException , NoSuchAlgorithmException {
161- log .info ("/authorize scope={} response_type={} client_id={} redirect_uri={}" , scope , response_type , client_id , redirect_uri );
167+ UriComponentsBuilder uriBuilder ,
168+ HttpServletRequest req ) throws JOSEException , NoSuchAlgorithmException {
169+ log .info ("called " + AUTHORIZATION_ENDPOINT +" from {}, scope={} response_type={} client_id={} redirect_uri={}" ,
170+ req .getRemoteHost (), scope , response_type , client_id , redirect_uri );
162171 if (auth == null ) {
163172 log .info ("user and password not provided" );
164173 return response401 ();
@@ -168,9 +177,8 @@ public ResponseEntity<?> authorize(@RequestParam String client_id,
168177 String password = creds [1 ];
169178 User user = fakeOidcProperties .getUser ();
170179 if (user .getLogname ().equals (logname ) && user .getPassword ().equals (password )) {
171- log .info ("user {} correct" , logname );
180+ log .info ("password for user {} is correct" , logname );
172181 String iss = uriBuilder .replacePath ("/" ).build ().encode ().toUriString ();
173- String sub = user .getSub ();
174182 String access_token = createAccessToken (iss , user , client_id , scope );
175183 String id_token = createIdToken (iss , user , client_id , nonce , access_token );
176184 String url = redirect_uri + "#" +
@@ -189,7 +197,7 @@ public ResponseEntity<?> authorize(@RequestParam String client_id,
189197
190198 private String createAccessToken (String iss , User user , String client_id , String scope ) throws JOSEException {
191199 // create JWT claims
192- Date expiration = new Date (System .currentTimeMillis () + 10 * 3600 * 1000L );
200+ Date expiration = new Date (System .currentTimeMillis () + fakeOidcProperties . getTokenExpirationSeconds () * 1000L );
193201 JWTClaimsSet jwtClaimsSet = new JWTClaimsSet .Builder ()
194202 .subject (user .getSub ())
195203 .issuer (iss )
@@ -222,7 +230,7 @@ private String createIdToken(String iss, User user, String client_id, String non
222230 .issuer (iss )
223231 .audience (client_id )
224232 .issueTime (new Date ())
225- .expirationTime (new Date (System .currentTimeMillis () + 10 * 3600 * 1000L ))
233+ .expirationTime (new Date (System .currentTimeMillis () + fakeOidcProperties . getTokenExpirationSeconds () * 1000L ))
226234 .jwtID (UUID .randomUUID ().toString ())
227235 .claim ("nonce" , nonce )
228236 .claim ("at_hash" , encodedHash )
0 commit comments