# Authentication

> Documentation index: https://ridewithgps.com/api/v1/doc/llms.txt

You must authenticate most requests to the API. The API supports 2 schemes: OAuth and Basic authentication.

We strongly recommend that you use OAuth. Use Basic authentication if you are the only user of your application. If you use an organization account, see [Authentication for organizations](https://ridewithgps.com/api/v1/doc/organizations/authentication.md).

<a id="oauth"></a>
## OAuth authentication

Ride with GPS is an OAuth provider, and we recommend that you use OAuth if you plan to authenticate users other than yourself. The workflow is secure, and does not require users to share their Ride with GPS password with you.

### Configuration

On your [API clients management page](https://ridewithgps.com/api/api_clients), create or update an API client and specify one or more Redirect URIs (`redirect_uri`) pointing to endpoints you own. Your API client is then set up for OAuth, and its management page shows your OAuth client ID (`client_id`) and secret (`client_secret`). Never share them with anyone.

### Authorization flow

#### 1. Authorize

Your application links your user to the authorization web page hosted on the Ride with GPS website:

```html
<a href="https://ridewithgps.com/oauth/authorize
  ?client_id={client_id}
  &redirect_uri={redirect_uri}
  &response_type=code">
  Sign in with Ride with GPS
</a>
```

Where:

* `redirect_uri` must match one of the OAuth redirect URIs configured for your API client
* `client_id` is the OAuth client ID of your API client

Ride with GPS asks the user to sign in if needed, then shows your API client's name and asks them to authorize it to read and modify their content. When they click **Authorize**, Ride with GPS redirects them to your `redirect_uri` with an access grant:

```
{redirect_uri}?code={access_grant}
```

#### 2. Exchange the `access_grant` for an `access_token`

Using the access grant that was sent to your `redirect_uri`, your application fetches the `access_token`:

```json
// POST https://ridewithgps.com/oauth/token.json
{
  "grant_type": "authorization_code",
  "code": "{access_grant}",
  "client_id": "{client_id}",
  "client_secret": "{client_secret}",
  "redirect_uri": "{redirect_uri}"
}
```

> `redirect_uri` in request body must match the one used in the first step above.

```json
// 200 - OK
{
  "access_token": "Qnpa6Bif4gF9s6uU5YwjtxbaP3UzQxgfArDP7HXZj_5",
  "token_type": "Bearer",
  "scope": "user",
  "created_at": 946728000,
  "user_id": 123
}
```

* `user_id` is the Ride with GPS assigned id for the user who completed the authorization flow.
* `access_token` authenticates your requests, in an `Authorization` header:

```
// header
Authorization: Bearer {access_token}
```

#### 3. Make OAuth authenticated requests

Your application can then make requests on the Ride with GPS API with the `Authorization` header to authenticate as your user. For example to get more details about the user who just completed the OAuth flow:

Endpoint: [Returns the current user](https://ridewithgps.com/api/v1/doc/endpoints/users.md)

```json
// GET https://ridewithgps.com/api/v1/users/current.json
// Authorization: Bearer {access_token}
```

#### 4. Revoke an access token

To revoke an OAuth access token, send this request:

```json
// POST https://ridewithgps.com/oauth/revoke.json
{
  "client_id": "{client_id}",
  "client_secret": "{client_secret}",
  "token": "{access_token}"
}
```

```json
// 200 - OK
{}
```

The token is then revoked, and you can no longer use it to authenticate requests.

<a id="basic"></a>
## Basic authentication

Basic authentication suits single user applications, such as a script that reads your own account.

### 1. Get an authentication token for your account

#### User accounts

Knowing your email and password, get an authentication token associated with your API key. This request needs only your API key, no authentication:

Endpoint: [Creates an authentication token](https://ridewithgps.com/api/v1/doc/endpoints/authentication_tokens.md)

```json
// POST https://ridewithgps.com/api/v1/auth_tokens.json
// x-rwgps-api-key: {api_key}
{
  "user": {
    "email": "{email}",
    "password": "{password}"
  }
}
```

```json
// 201 - Created
{
  "auth_token": {
    "auth_token": "c0cc78875f250604f9abac97712e314c",
    "api_key": "2704a8df",
    "created_at": "2000-01-01T12:00:00Z",
    "updated_at": "2000-01-01T12:00:00Z",
    "user": {
      "id": 123,
      "email": "alex@example.com",
      "name": "Alex Rider",
      "created_at": "2000-01-01T12:00:00Z",
      "updated_at": "2000-01-01T12:00:00Z"
    }
  }
}
```

<a id="organizations"></a>
#### Organization accounts

If you use an organization account, you have no password, so you cannot use the endpoint above. See [Authentication for organizations](https://ridewithgps.com/api/v1/doc/organizations/authentication.md).

### 2. Make Basic authenticated requests

You can then authenticate with Basic authentication, using your `api_key` and `auth_token`:

```
// header
Authorization: Basic base64encode("{api_key}:{auth_token}")
```

With `curl`, pass `-u "$API_KEY:$AUTH_TOKEN"`.

Alternatively, send them as headers:

```
// headers
x-rwgps-api-key: {api_key}
x-rwgps-auth-token: {auth_token}
```
