Skip to content

Commit 78bc3e8

Browse files
author
Paulo Cabral
committed
Add js Auth example with client credential flow
1 parent 875ee48 commit 78bc3e8

File tree

5 files changed

+203
-0
lines changed

5 files changed

+203
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
CLIENT CREDENTIAL FLOW
2+
======================
3+
4+
# Scenario
5+
**Ideal for backend integrations** that do not require access to other Uphold user accounts.
6+
7+
For business usage only you may choose to use client credentials authentication. This requires manual approval from Uphold.
8+
9+
- Get Token.
10+
- List Assets
11+
12+
13+
## Stack
14+
- JS/ES6
15+
- `node` v13.14.0 +
16+
17+
## Setup
18+
- see the `package.json`
19+
- type: `npm install` or `yarn install`
20+
- Create a `.env` file based on the `.env.example` file and populate with the required DATA
21+
22+
23+
## RUN
24+
- node index.js
25+
26+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Dependencies.
3+
*/
4+
5+
import axios from "axios";
6+
import b64Pkg from "js-base64";
7+
import dotenv from "dotenv";
8+
import path from "path";
9+
import qs from "qs";
10+
dotenv.config({ path: path.resolve() + "/.env" });
11+
const { encode } = b64Pkg;
12+
13+
/**
14+
* Get Token.
15+
*/
16+
export async function getToken() {
17+
// auth encoded with client ID and Client Secret
18+
// set post options for axios
19+
const auth = encode(process.env.CLIENT_ID + ":" + process.env.CLIENT_SECRET);
20+
const url = `${process.env.BASE_URL}/oauth2/token`;
21+
22+
const options = {
23+
method: "POST",
24+
headers: {
25+
Authorization: "Basic " + auth,
26+
"content-type": "application/x-www-form-urlencoded",
27+
},
28+
data: qs.stringify({ grant_type: "client_credentials" }),
29+
url,
30+
};
31+
32+
const data = axios(options)
33+
.then((response) => {
34+
return response.data;
35+
})
36+
.catch((error) => {
37+
error.response.data.errors
38+
? console.log(JSON.stringify(error.response.data.errors, null, 2))
39+
: console.log(JSON.stringify(error, null, 2));
40+
throw error;
41+
});
42+
43+
return data;
44+
}
45+
46+
/**
47+
* Get assets.
48+
*/
49+
export async function getAssets(token) {
50+
try {
51+
const r = await axios.get(`${process.env.BASE_URL}/v0/assets`, {
52+
headers: {
53+
Authorization: `${token.token_type} ${token.access_token}`,
54+
},
55+
});
56+
return r.data;
57+
} catch (error) {
58+
console.log(JSON.stringify(error, null, 2));
59+
throw error;
60+
}
61+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Dependencies.
3+
*/
4+
5+
import { getAssets, getToken } from "./cc-flow.js";
6+
7+
(async () => {
8+
// Get `bearer` token from sandbox
9+
const token = await getToken();
10+
// just log the assets to see if the token works!
11+
console.log(await getAssets(token));
12+
})();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "uphold-ccf",
3+
"version": "0.0.1",
4+
"description": "Uphold Client Credential Flow Example",
5+
"main": "index.js",
6+
"type": "module",
7+
"dependencies": {
8+
"axios": "^0.20.0",
9+
"btoa": "^1.2.1",
10+
"dotenv": "^8.2.0",
11+
"js-base64": "^3.5.2",
12+
"qs": "^6.9.4",
13+
"require": "^2.4.20"
14+
},
15+
"scripts": {
16+
"run": "node index.js "
17+
}
18+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
amdefine@>=0.0.4:
6+
version "1.0.1"
7+
resolved "https://registry.yarnpkg.com/amdefine/-/amdefine-1.0.1.tgz#4a5282ac164729e93619bcfd3ad151f817ce91f5"
8+
integrity sha1-SlKCrBZHKek2Gbz9OtFR+BfOkfU=
9+
10+
async@~0.2.6:
11+
version "0.2.10"
12+
resolved "https://registry.yarnpkg.com/async/-/async-0.2.10.tgz#b6bbe0b0674b9d719708ca38de8c237cb526c3d1"
13+
integrity sha1-trvgsGdLnXGXCMo43owjfLUmw9E=
14+
15+
axios@^0.20.0:
16+
version "0.20.0"
17+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.20.0.tgz#057ba30f04884694993a8cd07fa394cff11c50bd"
18+
integrity sha512-ANA4rr2BDcmmAQLOKft2fufrtuvlqR+cXNNinUmvfeSNCOF98PZL+7M/v1zIdGo7OLjEA9J2gXJL+j4zGsl0bA==
19+
dependencies:
20+
follow-redirects "^1.10.0"
21+
22+
btoa@^1.2.1:
23+
version "1.2.1"
24+
resolved "https://registry.yarnpkg.com/btoa/-/btoa-1.2.1.tgz#01a9909f8b2c93f6bf680ba26131eb30f7fa3d73"
25+
integrity sha512-SB4/MIGlsiVkMcHmT+pSmIPoNDoHg+7cMzmt3Uxt628MTz2487DKSqK/fuhFBrkuqrYv5UCEnACpF4dTFNKc/g==
26+
27+
dotenv@^8.2.0:
28+
version "8.2.0"
29+
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
30+
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==
31+
32+
follow-redirects@^1.10.0:
33+
version "1.13.0"
34+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.0.tgz#b42e8d93a2a7eea5ed88633676d6597bc8e384db"
35+
integrity sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA==
36+
37+
js-base64@^3.5.2:
38+
version "3.5.2"
39+
resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-3.5.2.tgz#3cc800e4f10812b55fb5ec53e7cabaef35dc6d3c"
40+
integrity sha512-VG2qfvV5rEQIVxq9UmAVyWIaOdZGt9M16BLu8vFkyWyhv709Hyg4nKUb5T+Ru+HmAr9RHdF+kQDKAhbJlcdKeQ==
41+
42+
optimist@~0.3.5:
43+
version "0.3.7"
44+
resolved "https://registry.yarnpkg.com/optimist/-/optimist-0.3.7.tgz#c90941ad59e4273328923074d2cf2e7cbc6ec0d9"
45+
integrity sha1-yQlBrVnkJzMokjB00s8ufLxuwNk=
46+
dependencies:
47+
wordwrap "~0.0.2"
48+
49+
qs@^6.9.4:
50+
version "6.9.4"
51+
resolved "https://registry.yarnpkg.com/qs/-/qs-6.9.4.tgz#9090b290d1f91728d3c22e54843ca44aea5ab687"
52+
integrity sha512-A1kFqHekCTM7cz0udomYUoYNWjBebHm/5wzU/XqrBRBNWectVH0QIiN+NEcZ0Dte5hvzHwbr8+XQmguPhJ6WdQ==
53+
54+
require@^2.4.20:
55+
version "2.4.20"
56+
resolved "https://registry.yarnpkg.com/require/-/require-2.4.20.tgz#66cb6baaabb65de8a71d793f5c65fd184f3798b6"
57+
integrity sha1-Zstrqqu2XeinHXk/XGX9GE83mLY=
58+
dependencies:
59+
std "0.1.40"
60+
uglify-js "2.3.0"
61+
62+
source-map@~0.1.7:
63+
version "0.1.43"
64+
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.1.43.tgz#c24bc146ca517c1471f5dacbe2571b2b7f9e3346"
65+
integrity sha1-wkvBRspRfBRx9drL4lcbK3+eM0Y=
66+
dependencies:
67+
amdefine ">=0.0.4"
68+
69+
std@0.1.40:
70+
version "0.1.40"
71+
resolved "https://registry.yarnpkg.com/std/-/std-0.1.40.tgz#3678a5f65094d9e1b6b5e26edbfc0212b8342b71"
72+
integrity sha1-Nnil9lCU2eG2teJu2/wCErg0K3E=
73+
74+
uglify-js@2.3.0:
75+
version "2.3.0"
76+
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.3.0.tgz#2cdec16d378a8a2b6ecfb6989784cf8b7ae5491f"
77+
integrity sha1-LN7BbTeKiituz7aYl4TPi3rlSR8=
78+
dependencies:
79+
async "~0.2.6"
80+
optimist "~0.3.5"
81+
source-map "~0.1.7"
82+
83+
wordwrap@~0.0.2:
84+
version "0.0.3"
85+
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107"
86+
integrity sha1-o9XabNXAvAAI03I0u68b7WMFkQc=

0 commit comments

Comments
 (0)