33import org .apache .logging .log4j .LogManager ;
44import org .apache .logging .log4j .Logger ;
55import org .comroid .api .StreamSupplier ;
6- import org .comroid .webkit .oauth .OAuth ;
7- import org .comroid .webkit .oauth .client .Client ;
8- import org .comroid .webkit .oauth .client .ClientProvider ;
9- import org .comroid .webkit .oauth .model .OAuthError ;
10- import org .comroid .webkit .oauth .resource .Resource ;
11- import org .comroid .webkit .oauth .resource .ResourceProvider ;
12- import org .comroid .webkit .oauth .rest .request .AuthenticationRequest ;
13- import org .comroid .webkit .oauth .rest .request .TokenRequest ;
14- import org .comroid .webkit .oauth .user .OAuthAuthorization ;
156import org .comroid .restless .CommonHeaderNames ;
167import org .comroid .restless .HTTPStatusCodes ;
178import org .comroid .restless .REST ;
2011import org .comroid .restless .server .ServerEndpoint ;
2112import org .comroid .uniform .Context ;
2213import org .comroid .uniform .node .UniNode ;
14+ import org .comroid .util .Pair ;
2315import org .comroid .webkit .frame .FrameBuilder ;
2416import org .comroid .webkit .model .PagePropertiesProvider ;
17+ import org .comroid .webkit .oauth .OAuth ;
18+ import org .comroid .webkit .oauth .client .Client ;
19+ import org .comroid .webkit .oauth .client .ClientProvider ;
20+ import org .comroid .webkit .oauth .model .OAuthError ;
21+ import org .comroid .webkit .oauth .model .ValidityStage ;
22+ import org .comroid .webkit .oauth .resource .Resource ;
23+ import org .comroid .webkit .oauth .resource .ResourceProvider ;
24+ import org .comroid .webkit .oauth .rest .request .AuthenticationRequest ;
25+ import org .comroid .webkit .oauth .rest .request .TokenRequest ;
26+ import org .comroid .webkit .oauth .rest .request .TokenRevocationRequest ;
27+ import org .comroid .webkit .oauth .user .OAuthAuthorization ;
2528import org .intellij .lang .annotations .Language ;
2629
2730import java .net .URI ;
@@ -58,13 +61,13 @@ public REST.Response executeGET(Context context, REST.Header.List headers, Strin
5861 final String userAgent = headers .getFirst (CommonHeaderNames .USER_AGENT );
5962
6063 try {
61- // find session & account
62- Client account = context .requireFromContext (ClientProvider .class )
63- .findAccessToken (headers )
64- . getAuthorization ()
65- .getClient ( );
64+ // find client
65+ Client client = context .requireFromContext (ClientProvider .class )
66+ .findClient (headers )
67+ // throw with status code OK to send login frame
68+ .orElseThrow (() -> new RestEndpointException ( OK ) );
6669
67- String authorizationCode = completeAuthorization (account , authenticationRequest , context , service , userAgent );
70+ String authorizationCode = completeAuthorization (client , authenticationRequest , context , service , userAgent );
6871
6972 // assemble redirect uri
7073 query .put ("code" , authorizationCode );
@@ -110,7 +113,7 @@ public REST.Response executePOST(Context context, REST.Header.List headers, Stri
110113 AuthenticationRequest authenticationRequest = loginRequests .getOrDefault (requestId , null );
111114 URIQueryEditor query = new URIQueryEditor (authenticationRequest .getRedirectURI ());
112115
113- Client client ;
116+ Pair < Client , String > client ;
114117 try {
115118 client = context .requireFromContext (ClientProvider .class )
116119 .loginClient (email , login );
@@ -125,14 +128,18 @@ public REST.Response executePOST(Context context, REST.Header.List headers, Stri
125128 .orElseThrow (() -> new RestEndpointException (UNAUTHORIZED , "Service with ID " + clientID + " not found" ));
126129 String userAgent = headers .getFirst (CommonHeaderNames .USER_AGENT );
127130
128- String code = OAuthEndpoint .completeAuthorization (client , authenticationRequest , context , service , userAgent );
131+ String code = OAuthEndpoint .completeAuthorization (client . getFirst () , authenticationRequest , context , service , userAgent );
129132
130133 // assemble redirect uri
131134 query .put ("code" , code );
132135 if (authenticationRequest .state .isNonNull ())
133136 query .put ("state" , authenticationRequest .getState ());
134137
135- return new REST .Response (FOUND , query .toURI ());
138+ REST .Header .List response = new REST .Header .List ();
139+ response .add ("Location" , query .toURI ().toString ());
140+ response .add ("Set-Cookie" , client .getSecond ());
141+
142+ return new REST .Response (FOUND , response );
136143 }
137144 },
138145 TOKEN ("/token" ) {
@@ -146,6 +153,34 @@ public REST.Response executePOST(Context context, REST.Header.List headers, Stri
146153 return new REST .Response (OK , accessToken );
147154 }
148155 },
156+ TOKEN_REVOKE ("/token/revoke" ) {
157+ @ Override
158+ public REST .Response executePOST (Context context , REST .Header .List headers , String [] urlParams , UniNode body ) throws RestEndpointException {
159+ ClientProvider clientProvider = context .requireFromContext (ClientProvider .class );
160+ TokenRevocationRequest request = new TokenRevocationRequest (context , body );
161+
162+ ValidityStage validity ;
163+ if (request .tokenHint .isNull ()) {
164+ validity = clientProvider .findValidityStage (request .getToken ());
165+ } else switch (request .getTokenHint ()) {
166+ case "access_token" :
167+ validity = clientProvider .findAccessToken (request .getToken ());
168+ break ;
169+ case "refresh_token" :
170+ // fixme
171+ //validity = clientProvider.findAccessToken(request.getToken());
172+ throw new UnsupportedOperationException ("unsupported: refresh token" );
173+ default :
174+ throw new AssertionError ("invalid token hint: " + request .getTokenHint ());
175+ }
176+
177+ if (validity == null )
178+ throw new RestEndpointException (BAD_REQUEST , "Unknown Token" );
179+ if (validity .isValid () && !validity .invalidate ())
180+ throw new RestEndpointException (INTERNAL_SERVER_ERROR , "Could not invalidate token" );
181+ return new REST .Response (OK );
182+ }
183+ },
149184 USER_INFO ("/userInfo" ) {
150185 @ Override
151186 public REST .Response executeGET (Context context , REST .Header .List headers , String [] urlParams , UniNode body ) throws RestEndpointException {
0 commit comments