1
0
mirror of https://github.com/oceanprotocol/docs.git synced 2024-11-26 19:49:26 +01:00
docs/developers/provider/authentication-endpoints.md

115 lines
4.1 KiB
Markdown
Raw Normal View History

# Authentication Endpoints
Provider offers an alternative to signing each request, by allowing users to generate auth tokens. The generated auth token can be used until its expiration in all supported requests. Simply omit the signature parameter and add the AuthToken request header based on a created token.
2023-05-23 15:29:55 +02:00
Please note that if a signature parameter exists, it will take precedence over the AuthToken headers. All routes that support a signature parameter support the replacement, with the exception of auth-related ones (createAuthToken and deleteAuthToken need to be signed).
### Create Auth Token
**Endpoint:** `GET /api/services/createAuthToken`
2023-05-23 15:29:55 +02:00
**Description:** Allows the user to create an authentication token that can be used to authenticate requests to the provider API, instead of signing each request. The generated auth token can be used until its expiration in all supported requests.
2023-05-23 15:29:55 +02:00
**Parameters:**
2023-05-23 15:29:55 +02:00
* `address`: The Ethereum address of the consumer (Optional).
* `nonce`: A unique identifier for this request, to prevent replay attacks (Required).
* `signature`: A digital signature proving ownership of the `address`. The signature should be generated by signing the hashed concatenation of the `address` and `nonce` parameters (Required).
* `expiration`: A valid future UTC timestamp representing when the auth token will expire (Required).
2023-05-23 15:29:55 +02:00
**Curl Example:**
2023-05-23 15:29:55 +02:00
{% code overflow="wrap" %}
2023-05-23 15:29:55 +02:00
```
GET /api/services/createAuthToken?address=<your_address>&&nonce=<your_nonce>&&expiration=<expiration>&signature=<your_signature>
```
{% endcode %}
2023-05-23 15:29:55 +02:00
Inside the angular brackets, the user should provide the valid values for the request.
Response:
{% code overflow="wrap" %}
2023-05-23 15:29:55 +02:00
```
{"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NjAwNTMxMjksImFkZHJlc3MiOiIweEE3OGRlYjJGYTc5NDYzOTQ1QzI0Nzk5MTA3NUUyYTBlOThCYTdBMDkifQ.QaRqYeSYxZpnFayzPmUkj8TORHHJ_vRY-GL88ZBFM0o"}
```
{% endcode %}
2023-05-23 15:29:55 +02:00
#### Javascript Example:
```runkit nodeVersion="18.x.x"
const axios = require('axios');
const address = "0x7e2a2FA2a064F693f0a55C5639476d913Ff12D05"
const nonce = "1"
const signature = ""
const url = `http://provider.oceanprotocol.com/api/services/createAuthToken?address=${address}&nonce=${nonce}&expiration=<expiration>&signature=<your_signature>`;
axios.get(url).then(response => {
console.log(response.data);
}).catch(error => {
console.error(error);
});
```
#### Delete Auth Token
2023-05-23 15:29:55 +02:00
#### DELETE /api/services/deleteAuthToken
Allows the user to delete an existing auth token before it naturally expires.
Parameters
```
address: String object containing consumer's address (optional)
nonce: Integer, Nonce (required)
signature: String object containing user signature (signed message)
2023-05-23 15:29:55 +02:00
The signature is based on hashing the following parameters:
address + nonce
token: token to be expired
```
Returns: Success message if token is successfully deleted. If the token is not found or already expired, returns an error message.
#### Javascript Example:
2023-05-23 15:29:55 +02:00
{% code overflow="wrap" %}
```javascript
const axios = require('axios');
// Define the address, token, and signature
const address = '<your_address>'; // Replace with your address
const token = '<your_token>'; // Replace with your token
const signature = '<your_signature>'; // Replace with your signature
// Define the URL for the deleteAuthToken endpoint
const deleteAuthTokenURL = 'http://<provider_url>/api/services/deleteAuthToken'; // Replace with your provider's URL
// Make the DELETE request
axios.delete(deleteAuthTokenURL, {
data: {
address: address,
token: token
},
headers: {
'Content-Type': 'application/json',
'signature': signature
}
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log('Error:', error);
});
2023-05-23 15:29:55 +02:00
```
{% endcode %}
2023-05-23 15:29:55 +02:00
Replace `<provider_url>`, `<your_address>`, `<your_token>`, and `<your_signature>` with actual values. This script sends a DELETE request to the `deleteAuthToken` endpoint and logs the response. Please ensure that `axios` is installed in your environment (`npm install axios`).
2023-05-23 15:29:55 +02:00
#### Example Response:
2023-05-23 15:29:55 +02:00
```
{"success": "Token has been deactivated."}
```