Skip to content

Commit

Permalink
Add sample code to use client certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamaguchi committed Jul 26, 2022
1 parent 01e0232 commit 73516d7
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 13 deletions.
6 changes: 6 additions & 0 deletions javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ let accsessToken = "ya29.a0AfH6SMCCXiqb-VmA6XMquR.....";

```
$ node addresses.js
```

If you use self-signed client certificate, set flag `NODE_TLS_REJECT_UNAUTHORIZED` to 0

```
NODE_TLS_REJECT_UNAUTHORIZED='0' node index.js
```
12 changes: 12 additions & 0 deletions javascript/addresses.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
let TapyrusApi = require('tapyrus_api');
const https = require('node:https');
const http = require('node:http');
const fs = require('node:fs');

let accsessToken = "";
let defaultClient = TapyrusApi.ApiClient.instance;
Expand All @@ -7,6 +10,15 @@ defaultClient.defaultHeaders = { Authorization: `Bearer ${accsessToken}` }
// You can change the host name and port number
// defaultClient.basePath = 'http://localhost:3000/api/v1'

// Client certificate
if (fs.existsSync('user.pfx')) {
const options = {
pfx: fs.readFileSync('user.p12'),
passphrase: '1234'
};
httpsAgent = new https.Agent(options);
defaultClient.requestAgent = httpsAgent;
}

// @route POST /api/v1/addresses
// @desc Generate new Address
Expand Down
61 changes: 48 additions & 13 deletions javascript/example-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ app.use(express.urlencoded({extended: true}));
const port = 3000;
const tapyrusApiHost = 'https://testnet-api.tapyrus.chaintope.com';


const { AuthorizationCode } = require('simple-oauth2');
const https = require('node:https');
const http = require('node:http');
const fs = require('node:fs');

const TapyrusApi = require('tapyrus_api');
const defaultClient = TapyrusApi.ApiClient.instance;
Expand All @@ -17,18 +19,51 @@ defaultClient.basePath = `${tapyrusApiHost}/api/v1`;
const client_id = '';
const client_secret = 'dummy';

const config = {
client: {
id: client_id,
secret: client_secret
},
auth: {
tokenHost: tapyrusApiHost,
tokenPath: 'oauth2/v1/token',
authorizeHost: tapyrusApiHost,
authorizePath: 'oauth2/v1/authorize'
}
};
let config = null;
// Client certificate
if (fs.existsSync('user.p12')) {
const options = {
pfx: fs.readFileSync('user.p12'),
passphrase: '1234'
};
httpsAgent = new https.Agent(options);
httpAgent = new http.Agent(options);
defaultClient.requestAgent = httpsAgent;
config = {
client: {
id: client_id,
secret: client_secret
},
auth: {
tokenHost: tapyrusApiHost,
tokenPath: 'oauth2/v1/token',
authorizeHost: tapyrusApiHost,
authorizePath: 'oauth2/v1/authorize'
},
http: {
agents: {
https: httpsAgent,
http: httpAgent,
httpsAllowUnauthorized: httpsAgent
}
}
};
} else {
config = {
client: {
id: client_id,
secret: client_secret
},
auth: {
tokenHost: tapyrusApiHost,
tokenPath: 'oauth2/v1/token',
authorizeHost: tapyrusApiHost,
authorizePath: 'oauth2/v1/authorize'
}
};
}



let client;
let accessToken;
Expand Down
12 changes: 12 additions & 0 deletions javascript/payments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
let TapyrusApi = require('tapyrus_api');
const https = require('node:https');
const http = require('node:http');
const fs = require('node:fs');

let accsessToken = "";
let defaultClient = TapyrusApi.ApiClient.instance;
Expand All @@ -7,6 +10,15 @@ defaultClient.defaultHeaders = { Authorization: `Bearer ${accsessToken}` }
// You can change the host name and port number
// defaultClient.basePath = 'http://localhost:3000/api/v1'

// Client certificate
if (fs.existsSync('user.p12')) {
const options = {
pfx: fs.readFileSync('user.p12'),
passphrase: '1234'
};
httpsAgent = new https.Agent(options);
defaultClient.requestAgent = httpsAgent;
}

// @route POST /api/v1/payment
// @desc Sending tpc
Expand Down
13 changes: 13 additions & 0 deletions javascript/timestamps.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
let TapyrusApi = require('tapyrus_api');
const https = require('node:https');
const http = require('node:http');
const fs = require('node:fs');

let accsessToken = "";
let defaultClient = TapyrusApi.ApiClient.instance;
Expand All @@ -7,6 +10,16 @@ defaultClient.defaultHeaders = { Authorization: `Bearer ${accsessToken}` }
// You can change the host name and port number
// defaultClient.basePath = 'http://localhost:3000/api/v1'

// Client certificate
if (fs.existsSync('user.p12')) {
const options = {
pfx: fs.readFileSync('user.p12'),
passphrase: '1234'
};
httpsAgent = new https.Agent(options);
defaultClient.requestAgent = httpsAgent;
}

let apiInstance = new TapyrusApi.TimestampApi();

// @route POST /api/v1/timestamp
Expand Down
13 changes: 13 additions & 0 deletions javascript/tokens.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
let TapyrusApi = require('tapyrus_api');
const https = require('node:https');
const http = require('node:http');
const fs = require('node:fs');

let accsessToken = "";
let defaultClient = TapyrusApi.ApiClient.instance;
Expand All @@ -7,6 +10,16 @@ defaultClient.defaultHeaders = { Authorization: `Bearer ${accsessToken}` }
// You can change the host name and port number
// defaultClient.basePath = 'http://localhost:3000/api/v1'

// Client certificate
if (fs.existsSync('user.p12')) {
const options = {
pfx: fs.readFileSync('user.p12'),
passphrase: '1234'
};
httpsAgent = new https.Agent(options);
defaultClient.requestAgent = httpsAgent;
}

let apiInstance = new TapyrusApi.TokenApi();


Expand Down
13 changes: 13 additions & 0 deletions javascript/users.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
let TapyrusApi = require('tapyrus_api');
const https = require('node:https');
const http = require('node:http');
const fs = require('node:fs');

let accsessToken = "";
let defaultClient = TapyrusApi.ApiClient.instance;
Expand All @@ -7,6 +10,16 @@ defaultClient.defaultHeaders = { Authorization: `Bearer ${accsessToken}` }
// You can change the host name and port number
// defaultClient.basePath = 'http://localhost:3000/api/v1'

// Client certificate
if (fs.existsSync('user.p12')) {
const options = {
pfx: fs.readFileSync('user.p12'),
passphrase: '1234'
};
httpsAgent = new https.Agent(options);
defaultClient.requestAgent = httpsAgent;
}

let apiInstance = new TapyrusApi.UserApi();


Expand Down

0 comments on commit 73516d7

Please sign in to comment.