feat(auth): Added Bearer basic auth, removed other auth patterns

This commit is contained in:
Chris Troutner
2025-11-25 08:46:18 -08:00
parent f80cce96b1
commit f24d36ccee
18 changed files with 58 additions and 268 deletions
+1
View File
@@ -15,6 +15,7 @@
"test:integration:nft": "export RESTURL=https://bchn.fullstack.cash/v6/ && export IS_USING_FREE_TIER=true && mocha --timeout 30000 -g '#nft1' test/integration/slp.js",
"test:integration:bchn": "export RESTURL=https://bchn.fullstack.cash/v6/ && export IS_USING_FREE_TIER=true && mocha --timeout 30000 test/integration/",
"test:integration:local:noauth": "export RESTURL=http://localhost:5942/v6/ && mocha --timeout 30000 test/integration/",
"test:integration:local:auth": "export RESTURL=http://localhost:5942/v6/ && export BCHJSBEARERTOKEN=some-random-token && mocha --timeout 30000 test/integration/",
"test:integration:decatur": "export RESTURL=http://192.168.2.127:5942/v6/ && mocha --timeout 30000 test/integration/",
"coverage": "nyc --reporter=html mocha --timeout 25000 test/unit/",
"docs": "./node_modules/.bin/apidoc -i src/ -o docs && ./fix-docs-contrast.sh",
+3 -14
View File
@@ -10,22 +10,11 @@ class Address {
else tmp.restURL = config.restURL
this.restURL = tmp.restURL
this.apiToken = tmp.apiToken
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
}
+9 -24
View File
@@ -47,40 +47,25 @@ class BCHJS {
this.restURL = process.env.RESTURL
} else this.restURL = DEFAULT_REST_API
// Retrieve the apiToken
this.apiToken = '' // default value.
if (config && config.apiToken && config.apiToken !== '') {
this.apiToken = config.apiToken
} else if (process.env.BCHJSTOKEN && process.env.BCHJSTOKEN !== '') {
this.apiToken = process.env.BCHJSTOKEN
// Retrieve the Bearer token for simple token authentication.
this.bearerToken = '' // default value.
if (config && config.bearerToken && config.bearerToken !== '') {
this.bearerToken = config.bearerToken
} else if (process.env.BCHJSBEARERTOKEN && process.env.BCHJSBEARERTOKEN !== '') {
this.bearerToken = process.env.BCHJSBEARERTOKEN
}
// Retrieve the Basic Authentication password.
this.authPass = '' // default value.
if (config && config.authPass && config.authPass !== '') {
this.authPass = config.authPass
} else if (process.env.BCHJSAUTHPASS && process.env.BCHJSAUTHPASS !== '') {
this.authPass = process.env.BCHJSAUTHPASS
}
// Generate a Basic Authentication token from an auth password
// Generate the authentication token for the authorization header.
this.authToken = ''
if (this.authPass) {
// console.log(`bch-js initialized with authPass: ${this.authPass}`)
// Generate the header for Basic Authentication.
const combined = `fullstackcash:${this.authPass}`
const base64Credential = Buffer.from(combined).toString('base64')
this.authToken = `Basic ${base64Credential}`
if (this.bearerToken) {
this.authToken = `Bearer ${this.bearerToken}`
}
const libConfig = {
restURL: this.restURL,
apiToken: this.apiToken,
authToken: this.authToken
}
// console.log(`apiToken: ${this.apiToken}`)
// ElectrumX indexer
this.Electrumx = new Electrumx(libConfig)
+3 -16
View File
@@ -10,26 +10,13 @@ import axios from 'axios'
class Blockchain {
constructor (config) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
// _this = this
}
/**
+3 -16
View File
@@ -9,26 +9,13 @@ import axios from 'axios'
class Control {
constructor (config) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
// _this = this
}
/**
+3 -14
View File
@@ -5,23 +5,12 @@ let _this
class DSProof {
constructor (config) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.authToken = config.authToken
this.axios = axios
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
+3 -17
View File
@@ -14,30 +14,16 @@ import Blockchain from './blockchain.js'
class ElectrumX {
constructor (config) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
// Encapsulate dependencies
this.blockchain = new Blockchain(config)
// this.address = new Address(config)
// _this = this
}
/**
+3 -14
View File
@@ -9,23 +9,12 @@ let _this
class Encryption {
constructor (config) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.axios = axios
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
+3 -16
View File
@@ -5,26 +5,13 @@ import axios from 'axios'
class Generating {
constructor (config) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
// _this = this
}
async generateToAddress (blocks, address, maxtries = 1000000) {
+3 -16
View File
@@ -5,26 +5,13 @@ import axios from 'axios'
class Mining {
constructor (config) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
// _this = this
}
async getBlockTemplate (templateRequest) {
+3 -16
View File
@@ -4,25 +4,12 @@ import axios from 'axios'
class Price {
constructor (config) {
// _this = this
this.restURL = config.restURL
this.apiToken = config.apiToken
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
+3 -16
View File
@@ -18,30 +18,17 @@ import SlpUtils from './slp/utils.js'
class PsfSlpIndexer {
constructor (config = {}) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
// Encapsulate dependencies
this.rawTransaction = new RawTransaction(config)
this.slpUtils = new SlpUtils(config)
// _this = this
}
/**
+3 -17
View File
@@ -5,29 +5,15 @@ import axios from 'axios'
class RawTransactions {
constructor (config) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
// Encapsulate dependencies
this.axios = axios
// this = this
}
/**
+3 -14
View File
@@ -3,22 +3,11 @@ import schnorr from 'bip-schnorr'
class Schnorr {
constructor (config) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
}
+3 -14
View File
@@ -19,22 +19,11 @@ import Utils from './utils.js'
class SLP {
constructor (config) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
+3 -14
View File
@@ -21,22 +21,11 @@ let _this // local global
class TokenType1 {
constructor (config) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
+3 -14
View File
@@ -12,24 +12,13 @@ let _this
class Utils {
constructor (config = {}) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.slpParser = slpParser
this.authToken = config.authToken
this.axios = axios
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
+3 -16
View File
@@ -5,26 +5,13 @@
class Util {
constructor (config) {
this.restURL = config.restURL
this.apiToken = config.apiToken
this.authToken = config.authToken
if (this.authToken) {
// Add Basic Authentication token to the authorization header.
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
} else {
// Add JWT token to the authorization header.
this.axiosOptions = {
headers: {
authorization: `Token ${this.apiToken}`
}
this.axiosOptions = {
headers: {
authorization: this.authToken
}
}
// _this = this
}
/**