OAuth 2.0 Integration
This page discusses Stardog’s support for OAuth 2.0 JWT Tokens as a means for authenticating users.
Page Contents
Overview
Stardog can use OAuth 2.0 JWT Tokens for authentication. While Stardog can validate tokens it has no means to fetch a new token from an Identity Provider (IDP) this is normally done by a web application or authentication proxy. This document describes the configuration changes that are needed to allow Stardog to issue JWT Tokens for users and optionally trust tokens issued by a given IDP.
Stardog expects the JWT identity token to be in the Authorization
HTTP header with the format bearer <token>
.
Configuring Stardog
By default, starting in version 7.8.0, Stardog is configured to issue authentication tokens. In order to provide customization and to increase the security of authentication token by providing secrets you will need to include the following property in stardog.properties
:
Property | Updatable | Description |
---|---|---|
jwt.conf | No | Absolute path to a valid jwt.yaml file |
The jwt.conf
property must point to a valid YAML file with the following schema:
confVersion: string # The version of the configuration file currently `1.0`
deploymentName: string # (optional) Name for this deployment
# Singer is required. This allows Stardog to issue tokens to be used instead of Basic Auth.
signer:
algorithm: [ HS256 | HS384 | HS512 | RS256 | RS384 | RS512 ] # Algorithm type to sign with.
audience: string # (optional) audience to include as `aud` field, default "stardog"
issuer: string # This is the name of the local issuer and will be automatically added to `issuers`
secret: string # (Required for HMAC only) secret key used to sign tokens.
publicKey: string # (Required for RSA only) Absolute path to an RSA public key.
privateKey: string # (Required for RSA only) Absolute path to an RSA private key.
tokenTTL: string # (optional) ISO-8601 duration format https://en.wikipedia.org/wiki/ISO_8601, default "P7D"
# Issuers are optional and there should be an entry for each issuer that is trusted.
issuers:
[ url ]: # The URL of the IDP to trust should match the `iss` field. Example `https://url-of-idp-to-trust`
audience: string # (required) limit trust to tokens issued for this audience in the `aud` field.
usernameField: string # (optional) the field to use as the username, defaults to `sub` field.
autoCreateUsers: boolean # (optional) `true` will create new users in Stardog, defaults to `false`.
allowedGroupIdentifiers: array # (optional) the groups that are allowed to be mapped to Stardog roles.
# algorithms object is optional by default Stardog will use the url of the issuer to query the
# issuers public key information at the address `<issuer url>/.well-known/jwks.json`.
algorithms:
[ HS256 | HS384 | HS512 ]: # Valid HMAC encryption types, see the `signer` fields
secret: string # (required) secret key used to sign tokens.
[ RS256 | RS384 | RS512 ]: # Valid RSA encryption types
# either keyUrl or publicKey must be provided but not both
keyUrl: string # URL to fetch jwks signing information from.
publicKey: string # Absolute path to an RSA public key.
privateKey: string # Absolute path to an RSA private key.
The license uuid is used as part of the signing key, therefore, the same license must be used on every node in the cluster. If the license uuid is different on the nodes then the token will only work against the node that generated it.
Example
Here’s an example stardog.properties
file:
jwt.conf = /var/opt/stardog/jwt.yaml
This properties file points to a valid YAML file specified by the jwt.conf
property. Here is an example:
---
confVersion: "1.0"
deploymentName: stardog-server
# This allows Stardog to issue a token for authentication against itself.
signer:
algorithm: HS256
audience: my-stardog
secret: "this-should-be-randomly-generated-secret-key"
issuer: http://my-domain.com
tokenTTL: "P30D"
issuers:
# An example of Azure auth that requires a special `keyUrl`
https://login.microsoftonline.com/<azure-tenant-id>/v2.0:
usernameField: email
audience: <azure-client-id>
algorithms:
RS256:
keyUrl: https://login.microsoftonline.com/<azure-tenant-id>/discovery/v2.0/keys
# An example using apps.stardog.com
https://apps.stardog.com:
usernameField: username
audience: https://fqdn.of.stardog:5820
# An example of an IDP that supports the '.well-known' urls
https://my-domain.auth0.com/:
audience: <auth0-client-id>
usernameField: email
Usage
Stardog will issue a token if you provide a valid username and password with basic auth.
$ curl -u anonymous:anonymous https://express.stardog.cloud:5820/admin/token
{"token": "..."}
Once you have obtained a valid token you can call a properly configured Stardog via the HTTP API:
$ export BEARER_TOKEN=<insert valid token here>
$ curl -H "Authorization: bearer ${BEARER_TOKEN}" https://express.stardog.cloud:5820/admin/users
{"users": ["anonymous"]}
User Management
All users who wish to connect via an external IDP OAuth Token should either already exist in the Stardog User system or have the autoCreateUsers
property should set to true
. Existing usernames should match what is being returned in the usernameField
setting for the Issuer
.
When autoCreateUsers
is set to true
Stardog will attempt to create a new user with the value of the usernameField
claim. The token must also include at least one role for the user that already exists in Stardog. If a new user does not have an existing role specified a Stardog user will not be created.
See Managing Users and Roles for more information.
Role Mapping
The property allowedGroupIdentifiers
is used to limit the roles that can be used for mapping. Any role or group from a token claim must match one of the roles in allowedGroupIdentifiers
or the associated user will not be assigned any permissions in Stardog.
In the following example only the AD groups readers
and writers
will be allowed for matching to Stardog roles.
allowedGroupIdentifiers: [ readers,writers ]
Users that are successfully mapped to a role will be assigned the permissions of that role but will not be added to the role.
OAuth 2.0 Identity Providers
Stardog supports OAuth 2.0 authentication using tokens from supported identity providers. This section provides information about configuring and using them.
Azure Active Directory
Stardog can use OAuth 2.0 JWT Tokens generated by Azure AD using OpenId for authentication against existing users or optionally create new users. There are several ways known as flows that can be used to obtain tokens, this documentation explains how to setup AD authentication using authorization code flow and configure role-based access using a token with roles.
You can find more information on Azure Active Directory and the Microsoft identity platform here.
Configure Azure AD Application
If you have an existing Azure application you can skip these steps.
- In your Azure portal go to Home->App Registrations and click New Registration to add a new application.
- Take note of the Application (client) ID and the Directory (tenant) ID.
- Add any necessary client credentials and generate a client secret.
- Add a redirect URL of localhost.
- Click endpoints and note the V2 authorization endpoint URL.
- From the portal go to Home->Azure Active Directory->Enterprise Applications and confirm your application is there, add it if it is not.
- Click on your application and add your users with their desired application role. Create new roles as needed.
Azure Authentication and Token Creation
- Use your browser to navigate to the authorization endpoint URL that you noted above filling in your tenant and client ids and sign in using your application credentials.
https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/authorize?client_id=<client id>&response_type=id_token&scope=openid&nonce=<nonce value>&state=<state value>
URL Parameter | Value |
---|---|
<tenant_id> | The directory/tenant ID for your Azure account |
client_id | The application/client ID for your application |
response_type | id_token |
scope | openid |
nonce | Your nonce value |
- Capture the identity token that is returned in the redirect URL you specified while setting up your Azure application. The string value for URL parameter
id_token
is the token.
http://localhost/#id_token=<id token value>&session_state=01e749c6-459a-460b-a35e-42c5f122999d
Configure Stardog Server
This example JWT config expects to find existing Stardog user names in the preferred_username
token claim value.
confVersion: "1.0"
deploymentName: stardog-server
issuers:
https://login.microsoftonline.com/<azure-tenant-id>/v2.0:
audience: <azure-client-id>
usernameField: preferred_username
algorithms:
RS256:
keyUrl: https://login.microsoftonline.com/<azure-tenant-id>/discovery/v2.0/keys
Fill in the <azure-tenant-id>
and <azure-client-id>
with the values noted from above. Restart Stardog and authenticate using the token as instructed above.