Securing resources
One of the most common use cases for using the OneWelcome Identity Platform is to use APIs to securely expose sensitive data to mobile apps. The OneWelcome Identity Platform Mobile SDK uses the Bearer Token Usage RFC (rfc6750) to talk to these protected API endpoints. To protect your APIs, you can set up a resource gateway according to the specification.
The resource gateway has the following responsibilities:
- Extract the access token from a request
- Introspect the access token
- Verify the access level through the introspection result
- Verify the authentication method through the introspection result
- Fetch and return the secured resource to the client
- Handle errors according to the RFC
Example code
The example code shows how to implement these responsibilities in a simple Spring Boot application. For a production setup, you might consider using a third-party product or a more scalable solution. The complete example resource gateway is available as a public repository on GitHub.
The example code exposes three endpoints:
-
The
/resource/devices
endpoint returns a list of the registered user's devices from the OneWelcome Identity Platform. -
The
/resource/application-details
endpoint returns some general information about the client application coming from the OneWelcome Identity Platform. -
The
/resource/user-id-decorated
endpoint returns the current user's ID with some added affixes.
Request the devices endpoint
In the following scenario, the client requests the devices endpoint:
-
The client first performs a GET request to
/resources/devices
with an access token in theAuthorization
header. -
The resource gateway introspects the access token with the OneWelcome Identity Platform.
-
The OneWelcome Identity Platform responds to this request with information about the access token, including the user ID (
sub
) and the scopes associated with the token. Theread
scope is required to access the devices endpoint. The OneWelcome Identity Platform also provides an Authentication Method Reference (amr
) field. You can use theamr
field to view how a user authenticated. To deny access to users who are implicitly authenticated, check theamr
field for the device's endpoint. -
and 5. If the token introspection result confirmed that the required scope is present, the devices for the user assigned to the access token are fetched from the secured API, which is the OneWelcome Identity Platform device API.
-
Finally, the list of devices is returned to the client application.
Extract the access token from a request
The Mobile SDK provides the access token in the Authorization
header with the Bearer
prefix. The access token is included in plain text, which means that it has no encoding.
Example request:
The following example code extracts the access token from the HTTP header of the incoming request:
Introspect the access token
Send the received access token to the OneWelcome Identity Platform for introspection. For the API description, see the introspection API reference.
The following code executes the introspection requests and checks if the the token is valid according to the OneWelcome Identity Platform:
The TokenIntrospectionRequestExecutor
executes the actual HTTP request:
Verify the access level
To check whether the required access level is met, which means whether the user gave consent for certain scopes to the application, the scopes need to be validated. In this case the read
scope is required. This example also includes a method to validate whether the application-details
scope is granted.
Verify the authentication method
The TokenTypeValidationService
class verifies whether the user authenticated using implicit authentication.
In this scenario, we do not want implicitly authenticated users to access the devices API. To check whether the user is not implicitly authenticated, the controller can pass the Authentication Method Reference (amr) values from the introspection result:
Fetch and return the secured resource
After all checks are passed successfully, the devices for the user in the access token can be fetched and returned to the original caller. The device API can be reached using the URI: /api/v4/users/{userId}/devices
.
Alternatively, you can request a resource that is not user-specific. For example, this might be useful if you have some content that you don't want to be publicly available on the web, and want to share only via a mobile app. To accomplish this, perform an anonymous resource call. This call uses an access token that does not have a user assigned. For an application to be able to receive such an access token, it needs to have the Anonymous resource calls
flow enabled in the OneWelcome Identity Platform.
The example resource gateway exposes an /resources/application-details
endpoint that returns the details of an application like the application version. This information is fetched from the access token introspection result and can be used with a user and an anonymous access token. A prerequisite is that the application-details
scope is granted to the access token that is used.
Handle errors according to the RFC
The RFC defines specific errors for certain error scenarios, see the Bearer Token Usage Error Codes RFC (rfc6750 par 3.1).