|
| 1 | +# Machine-to-Machine (M2M) Token Guide for Topcoder Review API |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The Topcoder Review API supports machine-to-machine (M2M) authentication using Auth0 for service-to-service integrations. |
| 6 | +This guide explains how to use M2M tokens with the API. |
| 7 | + |
| 8 | +## What are M2M Tokens? |
| 9 | + |
| 10 | +Machine-to-Machine tokens are designed for service-to-service authentication where a human user is not directly involved. |
| 11 | +Instead of using user roles for authorization, M2M tokens use scopes, which are permissions attached to the token. |
| 12 | + |
| 13 | +## M2M Token Structure |
| 14 | + |
| 15 | +M2M tokens from Auth0 contain the following important claims: |
| 16 | +- `iss` (issuer): The Auth0 domain that issued the token |
| 17 | +- `sub` (subject): The client ID of the application |
| 18 | +- `aud` (audience): The API identifier (audience) |
| 19 | +- `exp` (expiration time): When the token expires |
| 20 | +- `iat` (issued at time): When the token was issued |
| 21 | +- `scope`: Space-separated list of authorized scopes |
| 22 | + |
| 23 | +## Available Scopes |
| 24 | + |
| 25 | +The Topcoder Review API supports the following scopes: |
| 26 | + |
| 27 | +### Group Scopes |
| 28 | +- `read:groups` - Do read action of groups |
| 29 | +- `write:groups` - Do write action of groups |
| 30 | +- `all:groups` - All groups-related operations |
| 31 | + |
| 32 | +## Getting an M2M Token |
| 33 | + |
| 34 | +To get an M2M token, you need to have a client registered in Auth0 with the appropriate permissions. |
| 35 | + |
| 36 | +### Example Request |
| 37 | + |
| 38 | +```bash |
| 39 | +curl --request POST \ |
| 40 | + --url https://topcoder-dev.auth0.com/oauth/token \ |
| 41 | + --header 'content-type: application/json' \ |
| 42 | + --data '{ |
| 43 | + "client_id": "YOUR_CLIENT_ID", |
| 44 | + "client_secret": "YOUR_CLIENT_SECRET", |
| 45 | + "audience": "https://m2m.topcoder-dev.com/", |
| 46 | + "grant_type": "client_credentials" |
| 47 | + }' |
| 48 | +``` |
| 49 | + |
| 50 | +### Example Response |
| 51 | + |
| 52 | +```json |
| 53 | +{ |
| 54 | + "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6...", |
| 55 | + "scope": "read:appeal create:review all:scorecard", |
| 56 | + "expires_in": 86400, |
| 57 | + "token_type": "Bearer" |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## Using the Token |
| 62 | + |
| 63 | +Include the token in your API requests in the Authorization header: |
| 64 | + |
| 65 | +``` |
| 66 | +Authorization: Bearer YOUR_ACCESS_TOKEN |
| 67 | +``` |
| 68 | + |
| 69 | +## Scope Validation |
| 70 | + |
| 71 | +When a request is made to the API, the token's scopes are validated against the required scopes for the endpoint. |
| 72 | +If the token has at least one of the required scopes, access is granted. |
| 73 | + |
| 74 | +### Notes on "all:" Scopes |
| 75 | + |
| 76 | +Scopes that start with "all:" automatically grant access to all the specific operations in that category. |
| 77 | +For example, `all:groups` includes `read:groups`, `write:groups`, etc. |
| 78 | + |
| 79 | +## Testing M2M Tokens |
| 80 | + |
| 81 | +For testing locally, you can use the following predefined test tokens: |
| 82 | + |
| 83 | +- `m2m-token-all` - Has all available scopes |
| 84 | +- `m2m-token-groups` - Has all groups scopes |
| 85 | + |
| 86 | +Example usage with curl: |
| 87 | + |
| 88 | +```bash |
| 89 | +curl -X GET http://localhost:3000/v6/groups \ |
| 90 | + -H "Authorization: Bearer m2m-token-review" |
| 91 | +``` |
| 92 | + |
| 93 | +## Troubleshooting |
| 94 | + |
| 95 | +If you receive a 403 Forbidden response, check that: |
| 96 | +1. Your token is valid and not expired |
| 97 | +2. The token has the required scope for the endpoint |
| 98 | +3. You're using the correct audience and issuer |
0 commit comments