Problem description: I am trying to secure a resource with OAuth2 access authorization, where the path of the resource indicates ownership by a resource owner.
- That is to say I do not want simply give access to a Client based on general regex URL/Scope matching. I want requests coming from the Client to undergo a security constraint that matches the individual resource ownership of the Resource Server URL (e.g. indicated by a URL parameter) on the access token provided by the client.
- I want to understand if (and if yes, how) this can be done with only OAuth2. I have doubts, as it is an authorization protocol, not an authentication protocol. It seems for authentication purposes it is often combined with OpenID, so I wonder if that is what is blocking me here.
Extra requirements:
- I want to use the recommended Authorization Code grant type.
- I do not want to use an existing Authorization Server (GitHub, Google, etc…).
What I‘ve found: I found a tutorial and sample code by Baeldung. It works fine and is close to the setup I want, but it grants access using scopes, which are general URL regex matchers. That is to say in the referenced example the Client (service) obtains access for parts of the Resource Server‘s REST API, but there is no notion of user ownership for the resources, e.g. as part of the URL. That is to say, the resources in question seem to belong to the Resource Server admin, not individual service users.
Questions:
- Is my understanding of OAuth2 correct, that access delegation for resources owned by service users, is a valid use case?
E.g. can I grant access to a Client who wants to access
/{someuseridentifier}/someresource
on behalf of a service user:someuseridentifier
? That is to say, obtain a Client that when authorized foralan
can access/alan/someresource
, but notada/someresource
. - If that is possible, what is the clean way to do this? Intuitively I would implement a check in the REST endpoint that compares the
Principal
(token owner) to the value ofsomeuseridentifier
(being part of the URL). Is this the right way? Can I even retrieve thePrincipal
, as the person who originally granted access to the resource, or is this not supported by OAuth2? - If it is possible, is there is a better way than coding the comparison in the REST endpoints? could I use e.g. a
WebSecurityConfigurerAdapter
that intercepts inbound requests, and matchesPrincipal
on URL (and hence the actual resource owner).
I've read the OAuth2 specification, followed a setup tutorial, investigated the OAuth2 sample setup for access delegation and searched for similar issues on stackoverflow, but it seems no one else has this problem, although it is listed as common scenario in literature. I am able to configure general admin access delegation, using URL regex matching, but not on a service-user basis.