{
	"info": {
		"_postman_id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
		"name": "Betterez - Refresh token",
		"description": "Login with Basic Auth, then redeem a refresh token. Requires refresh tokens enabled on the application.",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
	},
	"item": [
		{
			"name": "01 - Login (get refresh token)",
			"event": [
				{
					"listen": "test",
					"script": {
						"type": "text/javascript",
						"exec": [
							"var jsonData = pm.response.json();",
							"if (jsonData.shortToken) {",
							"    pm.environment.set('shortToken', jsonData.shortToken);",
							"}",
							"if (jsonData.refreshToken) {",
							"    pm.environment.set('refreshToken', jsonData.refreshToken);",
							"} else {",
							"    pm.test('refreshToken is present (enable refresh tokens on the application)', function () {",
							"        pm.expect(jsonData.refreshToken).to.be.a('string');",
							"    });",
							"}",
							"if (jsonData.publicKey) {",
							"    pm.environment.set('publicKey', jsonData.publicKey);",
							"}",
							"pm.test('Status code is 200', function () {",
							"    pm.response.to.have.status(200);",
							"});"
						]
					}
				}
			],
			"request": {
				"method": "POST",
				"header": [
					{
						"key": "x-api-key",
						"value": "{{x-api-key}}"
					},
					{
						"key": "Content-Type",
						"value": "application/json"
					},
					{
						"key": "Authorization",
						"value": "Basic {{basicAuthToken}}"
					}
				],
				"body": {
					"mode": "raw",
					"raw": "{\n\t\"audience\": \"{{audience}}\"\n}"
				},
				"url": {
					"raw": "{{basePath}}/accounts/users",
					"host": [
						"{{basePath}}"
					],
					"path": [
						"accounts",
						"users"
					]
				},
				"description": "Authenticate a user with Basic Auth. Saves shortToken and refreshToken to the environment when refresh tokens are enabled on the application."
			},
			"response": []
		},
		{
			"name": "02 - Redeem refresh token",
			"event": [
				{
					"listen": "test",
					"script": {
						"type": "text/javascript",
						"exec": [
							"var jsonData = pm.response.json();",
							"if (jsonData.shortToken) {",
							"    pm.environment.set('shortToken', jsonData.shortToken);",
							"}",
							"if (jsonData.refreshToken) {",
							"    pm.environment.set('refreshToken', jsonData.refreshToken);",
							"}",
							"pm.test('Status code is 200', function () {",
							"    pm.response.to.have.status(200);",
							"});"
						]
					}
				}
			],
			"request": {
				"method": "POST",
				"header": [
					{
						"key": "x-api-key",
						"value": "{{x-api-key}}"
					},
					{
						"key": "Content-Type",
						"value": "application/json"
					}
				],
				"body": {
					"mode": "raw",
					"raw": "{\n\t\"refreshToken\": \"{{refreshToken}}\"\n}"
				},
				"url": {
					"raw": "{{basePath}}/accounts/refresh-token",
					"host": [
						"{{basePath}}"
					],
					"path": [
						"accounts",
						"refresh-token"
					]
				},
				"description": "Exchange a refresh token for a new access token. Only X-API-KEY is required. Each refresh token is single-use."
			},
			"response": []
		},
		{
			"name": "03 - Sample authenticated request",
			"event": [
				{
					"listen": "test",
					"script": {
						"type": "text/javascript",
						"exec": [
							"pm.test('Status code is 200', function () {",
							"    pm.response.to.have.status(200);",
							"});"
						]
					}
				}
			],
			"request": {
				"method": "GET",
				"header": [
					{
						"key": "x-api-key",
						"value": "{{x-api-key}}"
					},
					{
						"key": "Authorization",
						"value": "Bearer {{shortToken}}"
					}
				],
				"url": {
					"raw": "{{basePath}}/inventory/stations",
					"host": [
						"{{basePath}}"
					],
					"path": [
						"inventory",
						"stations"
					]
				},
				"description": "Example request using the shortToken from login or refresh."
			},
			"response": []
		}
	]
}
