@viewdo/dxp-story-cli
Version:
README.md
82 lines (71 loc) • 2.13 kB
JavaScript
const axios = require('axios')
const console = require('./console-service')
const {requireValue} = require('./utils')
module.exports = class AuthService {
constructor(
auth_api = requireValue('auth_api'),
auth0_api = requireValue('auth0_api'),
client_id = requireValue('client_id'),
client_secret = requireValue('client_secret'),
audience = requireValue('audience')) {
Object.assign(this, {
auth_api,
auth0_api,
client_id,
client_secret,
audience
})
}
// auth API ---------------------------------
getAppToken() {
let self = this
if (this.client_secret == undefined)
return Promise.resolve(false)
let {
auth0_api,
client_id,
client_secret,
audience } = this
let auth0_path = `${auth0_api}/oauth/token`
console.debug(`Attempting Application Authentication: ${auth0_path} for Client ID: ${client_id}`)
return axios.post(auth0_path, {
client_id,
client_secret,
audience,
grant_type: "client_credentials"
})
.then(auth0_response => {
let auth_path = `${self.auth_api}/v1/accounts/exchange?token=${auth0_response.data.access_token}`
console.debug(`API: POST ${auth_path}`.gray)
return axios.post(auth_path)
.then(ivx_response => {
console.debug('API: Using token')
if (ivx_response.status == 200)
return ivx_response.data.token
})
})
}
sendVerificationEmail(email) {
let { client_id } = this
let path = `${this.auth0_api}/passwordless/start`
console.debug(`API: POST ${path}`.gray)
return axios.post(path, {
client_id: client_id,
connection: 'email',
email,
send: 'code'
})
}
getTokenFromVerification(email, verification_code) {
let { client_id } = this
let path = `${this.auth_api}/v1/accounts/confirm-passwordless`
return axios.post(path, {
clientId: client_id,
code: verification_code,
email
})
.then(ivx_response => {
return ivx_response.data.token
})
}
}