Hi,
I always like to get feedback from pac4j users. Especially PR ;-)
However, pac4j is meant to handle web flows: you call a protected url, the protected url is saved in session, the authentication occurs successfully (redirect to and back from the identity provider) and the original url is restored. It relies on the web session to save the original url. It doesn't work very well for REST.
So I guess you do a web flow authentication on one url (based on pac4j) and then on another url make some REST call on your own authentication mechanism.
It's a feasible solution, but I think the real target would be to have a good support of REST in pac4j. And we are maybe not so far from it.
Let's dive deeper in details (for play-pac4j):
- we have protected urls and a callback url:
@RequiresAuthentication(clientName = "TwitterClient")
public static Result twitterIndex() {
...
@RequiresAuthentication(clientName = "BasicAuthClient")
public static Result basicauthIndex() {
...
and the associated routes:
GET /twitter/index.html controllers.Application.twitterIndex()
GET /basicauth/index.html controllers.Application.basicauthIndex()
GET /callback org.pac4j.play.CallbackController.callback()
POST /callback org.pac4j.play.CallbackController.callback()
When a request comes for a protected url (@RequiresAuthentication annotation), the url is saved into session and a redirection happens to the identity provider. Then, after a successful authentication, the user is redirected back to the callback url with a parameter client_name (pac4j client) and specific credentials from the identity provider. The authentication is finished by the callback controller.
For a basic auth authentication, the same occurs except that the redirection to the identity provider is replaced by a direct redirection to the callback url which returns a 401 if no basic auth header is found and otherwise plays the authentication if a basic auth header is found.
The callback controller finishes the authentication and redirects to the original url. It's perfect for a web flow, but for a REST call, we just want the callback controller to finish the authentication and pass to the application logic.
Somehow, we could already have:
@CallbackController
public static Result callback() {
return redirectToOriginalUrl();
}
Which would become for a REST endpoint:
@CallbackController
public static Result restEndpoint() {
// application continues logic
...
}
With this change, REST calls would be supported out of the box. Just add the @CallbackController annotation (or function in Scala) to enable a REST endpoint. Maybe it should be renamed as @AuthenticationEndpoint.
2) If you want to expose a REST API, I think I already answered above. You already have the access token as part of the FacebookProfile (as well as the other OAuth profiles).
I hope these long explanations make things clearer.
Thanks.
Best regards,