11/*
2- * Copyright (C) 2017 Authlete, Inc.
2+ * Copyright (C) 2017-2023 Authlete, Inc.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2929import com .authlete .common .api .AuthleteApiFactory ;
3030import com .authlete .common .web .BasicCredentials ;
3131import com .authlete .jaxrs .BaseIntrospectionEndpoint ;
32+ import com .authlete .jaxrs .IntrospectionRequestHandler .Params ;
33+ import com .authlete .jaxrs .server .db .ResourceServerDao ;
34+ import com .authlete .jaxrs .server .db .ResourceServerEntity ;
3235
3336
3437/**
3942 * >RFC 7662, OAuth 2.0 Token Introspection</a>
4043 *
4144 * @author Takahiko Kawasaki
45+ * @author Hideki Ikeda
4246 */
4347@ Path ("/api/introspection" )
4448public class IntrospectionEndpoint extends BaseIntrospectionEndpoint
@@ -53,6 +57,7 @@ public class IntrospectionEndpoint extends BaseIntrospectionEndpoint
5357 @ Consumes (MediaType .APPLICATION_FORM_URLENCODED )
5458 public Response post (
5559 @ HeaderParam (HttpHeaders .AUTHORIZATION ) String authorization ,
60+ @ HeaderParam (HttpHeaders .ACCEPT ) String accept ,
5661 MultivaluedMap <String , String > parameters )
5762 {
5863 // "2.1. Introspection Request" in RFC 7662 says as follows:
@@ -68,46 +73,47 @@ public Response post(
6873 // Therefore, this API must be protected in some way or other.
6974 // Basic Authentication and Bearer Token are typical means, and
7075 // both use the value of the 'Authorization' header.
71- //
72- // Authenticate the API caller.
73- boolean authenticated = authenticateApiCaller (authorization );
7476
75- // If the API caller does not have necessary privileges to call this API.
76- if (authenticated == false )
77+ BasicCredentials credentials = BasicCredentials .parse (authorization );
78+
79+ // Fetch the information about the resource server from DB.
80+ ResourceServerEntity rsEntity = ResourceServerDao .get (credentials .getUserId ());
81+
82+ // If failed to authenticate the resource server.
83+ if (authenticateResourceServer (rsEntity , credentials ) == false )
7784 {
7885 // Return "401 Unauthorized".
7986 return Response .status (Status .UNAUTHORIZED ).build ();
8087 }
8188
89+ // Build a Param object to call the request handler.
90+ Params params = buildParams (parameters , accept , rsEntity );
91+
8292 // Handle the introspection request.
83- return handle (AuthleteApiFactory .getDefaultApi (), parameters );
93+ return handle (AuthleteApiFactory .getDefaultApi (), params );
8494 }
8595
8696
87- /**
88- * Authenticate the API caller.
89- *
90- * @param authorization
91- * The value of the {@code Authorization} header of the API call.
92- *
93- * @return
94- * True if the API caller has necessary privileges to access
95- * the introspection endpoint.
96- */
97- private boolean authenticateApiCaller (String authorization )
97+ private Params buildParams (
98+ MultivaluedMap <String , String > parameters , String accept , ResourceServerEntity rsEntity )
9899 {
99- // TODO: This implementation is for demonstration purpose only.
100+ return new Params ()
101+ .setParameters (parameters )
102+ .setHttpAcceptHeader (accept )
103+ .setRsUri (rsEntity .getUri ())
104+ .setIntrospectionSignAlg (rsEntity .getIntrospectionSignAlg ())
105+ .setIntrospectionEncryptionAlg (rsEntity .getIntrospectionEncryptionAlg ())
106+ .setIntrospectionEncryptionEnc (rsEntity .getIntrospectionEncryptionEnc ())
107+ .setPublicKeyForEncryption (rsEntity .getPublicKeyForIntrospectionResponseEncryption ())
108+ .setSharedKeyForSign (rsEntity .getSharedKeyForIntrospectionResponseSign ())
109+ .setSharedKeyForEncryption (rsEntity .getSharedKeyForIntrospectionResponseEncryption ());
110+ }
100111
101- // If the Authorization header contains "Basic Authentication" and
102- // if the user part is "nobody".
103- BasicCredentials credentials = BasicCredentials .parse (authorization );
104- if (credentials != null && "nobody" .equals (credentials .getUserId ()))
105- {
106- // Reject the introspection request by "nobody".
107- return false ;
108- }
109112
110- // Accept anybody except "nobody".
111- return true ;
113+ private boolean authenticateResourceServer (
114+ ResourceServerEntity rsEntity , BasicCredentials credentials )
115+ {
116+ return rsEntity != null &&
117+ rsEntity .getSecret ().equals (credentials .getPassword ());
112118 }
113119}
0 commit comments