Out-of-band Token Verification

Verifying DC/OS Authentication tokens out-of-band

Other services can authenticate incoming requests on behalf of the DC/OS Identity and Access Manager (IAM) component, using public key cryptography. This works if the authentication token presented by the client has been signed by the IAM with its private key.

Bouncer JSON Web Key Set (JWKS) endpoint

The Bouncer’s JWKS endpoint (/auth/jwks) provides the public key details required for verifying the signature of type RS256 JWTs issued by Bouncer. The JSON document data structure emitted by that endpoint is compliant with RFC 7517. Within that data structure, the public key is parameterized according to RFC 7518.

Here is an example response:

  1. curl -k https://<host-ip/acs/api/v1/auth/jwks
  2. {
  3. "keys": [
  4. {
  5. "e": "AQAB",
  6. "use": "sig",
  7. "n": "7dYvibxUngEyfdut1uSYbRCCP5dT5MQyMfLyy_6o5x8PD-fUMgkm0vGUJAUoKimnkZ85aUmswaU3yAxQiZ8yeaoSpgUR4WJCRhOIEJ6Oyq4mjK06vr9-wJj5gVXDBaqbxD0yhgzMHEDyxg3EFOJ2ve73Vkg4p7pygA4fI_de1Bs6n68Hwt9LJ7B-fPg0PU8IdPe_4dYNuHT09KGxWSlq3m4KSvNxPIGQ8nNK9H3gjQaoBT9-hDXfsAgrQo7GenXRZTYW13KATtRAR5Vtd177iEeVefbK3HRj9IfYjYPnlBP2CZv_YIK-9H_33JPXxlDTFgI92l_JKRF-fPSa1EEkIw",
  8. "alg": "RS256",
  9. "kid": "49f795b26f80bec01f44b0f52e6ba6459ee2048fbb342f861f1a4e8ed4ebcb7f",
  10. "kty": "RSA"
  11. }
  12. ]
  13. }

Constructing the public key from the JWKS data

The two parameters that fully define an RSA public key are the modulus (n) and the exponent (e). Both are integers encoded using Base64 as specified in RFC 7518

Use the tool of your choice to generate the public key representation that you will need to validate the authentication token.

Here is a Python example based on the cryptography module (which uses OpenSSL as its back-end). This example generates a public key object from a given exponent and modulus directly.

  1. from cryptography.hazmat.backends import default_backend
  2. from cryptography.hazmat.primitives.asymmetric import rsa
  3. # `modulus_int` and `exponent_int` are the two parameters defining
  4. # an RSA public key. These are objects of type `int`.
  5. public_numbers = rsa.RSAPublicNumbers(n=modulus_int, e=exponent_int)
  6. public_key = public_numbers.public_key(backend=default_backend())

Verifying the authentication token using the public key

This example uses the Python PyJWT module, auth token verification, and extraction of the user ID:

  1. # `token` is the string holding the authentication token.
  2. # `public_key` is an RSA public key object of the cryptography module.
  3. payload = jwt.decode(token, public_key, algorithm='RS256')
  4. uid = payload['uid']

The decode method verifies the token signature and expiration time and raises an exception if the token is invalid.

Complete token verification example

This example validates an authentication token. Here is the example token

  1. eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjE0Njc5ODU0NjYsInVpZCI6InBldGVyIn0.lsLJx2WsX99HF96CizMOcZpMIgbjGDBHvFZCGeNDsM-xZQzHQJHo_UA8WodQ52o8uBJ2CY983DhJdIH2Gfc_fbZtYGvUx-IvQnHFbUBd8qBN0A_4BQHeNINFUKdVQuJsbsW-uVj-w0q3RAFwO5DPPc2ppwIjkeQbgGP1ZN-2-uV6Jow04cdkq4jcODsD1y0v4EmIBPLQil0HU2B95IHtlBNN7haTUkCksXE-43BHy4ErboySeq6VgkwLpw_Pi8n236kZ2-GobSmhA-BpjbkO3uGLHrYUfJjrJyiPM2_PZQMHY80-m5sMMMQ9m1Ciag2Cw74JKGfJ3qMW3j3z2Hm7GQ

Here is Python code for performing the validation, following the instructions given above:

  1. >>> import jwt
  2. >>> import requests
  3. >>> from cryptography.hazmat.backends import default_backend
  4. >>> from cryptography.hazmat.primitives.asymmetric import rsa
  5. >>> from jwt.utils import base64url_decode, bytes_to_number
  6. >>> keys = requests.get('http://localhost:8101/acs/api/v1/auth/jwks').json()['keys'][0]
  7. >>> keys
  8. {'kty': 'RSA', 'n': 'sybUYxu3TXxXAgG_Eq72tKxE7xhGFgL14g5OGryDtE5dBL8frAoSsI4D7tSKR2pLbOlT68YJbYLUHxoju0E_NB9htjKEsay4t3WXoXQ-XsDM4Zz22H6HfDG6CCcvGb2DoQP0R2je1HJDA56_BoR8shZMxHbrX1WgQURtGygMD7bQY95qmHZYRPlq13-pR5Jnu70OMmFlbl-_o-ag1JfndTJPtx75IalCgy_h_itHLDPhdTfypAJeiewCOUZd9nNa1j19M-xeqlZonlRABqiH0e-vQVWCeW5FZ0HJamIjd2VifhRCp0fSAgCdCQdrY6HdI3h6egpn6z4gwkwXBfczww', 'kid': '55fb61042768f62ea3b06778c6043f7c8c92769a0c248076a2995dfd50c4acb9', 'use': 'sig', 'alg': 'RS256', 'e': 'AQAB'}
  9. >>> exponent_bytes = base64url_decode(keys['e'].encode('ascii'))
  10. >>> exponent_int = bytes_to_number(exponent_bytes)
  11. >>> modulus_bytes = base64url_decode(keys['n'].encode('ascii'))
  12. >>> modulus_int = bytes_to_number(modulus_bytes)
  13. >>> public_numbers = rsa.RSAPublicNumbers(n=modulus_int, e=exponent_int)
  14. >>> public_key = public_numbers.public_key(backend=default_backend())
  15. >>> authtoken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjE0Njc5ODU0NjYsInVpZCI6InBldGVyIn0.lsLJx2WsX99HF96CizMOcZpMIgbjGDBHvFZCGeNDsM-xZQzHQJHo_UA8WodQ52o8uBJ2CY983DhJdIH2Gfc_fbZtYGvUx-IvQnHFbUBd8qBN0A_4BQHeNINFUKdVQuJsbsW-uVj-w0q3RAFwO5DPPc2ppwIjkeQbgGP1ZN-2-uV6Jow04cdkq4jcODsD1y0v4EmIBPLQil0HU2B95IHtlBNN7haTUkCksXE-43BHy4ErboySeq6VgkwLpw_Pi8n236kZ2-GobSmhA-BpjbkO3uGLHrYUfJjrJyiPM2_PZQMHY80-m5sMMMQ9m1Ciag2Cw74JKGfJ3qMW3j3z2Hm7GQ"
  16. >>> payload = jwt.decode(authtoken, public_key, algorithm='RS256')
  17. >>> payload
  18. {'uid': 'peter', 'exp': 1467985466}

The response indicates that this is a valid authentication token for peter.