Skip to content
Permalink
Browse files

feat(auth): add methods to verify auth token

  • Loading branch information...
satello committed May 8, 2018
1 parent 2804501 commit c0bfb62fae0e6003c8cf903634a542f82f0985c5
@@ -80,3 +80,8 @@ export const MISSING_STORE_PROVIDER =
export const MISSING_CONTRACT_INSTANCE = contractAddress =>
`No contract instance stored for ${contractAddress}. Please call addContractInstance.`
export const ERROR_FETCHING_EVENTS = error => `Unable to fetch events: ${error}`

// Auth
export const UNABLE_TO_SIGN_TOKEN = `There was an error signing the auth token. Please try again`
export const INVALID_AUTH_TOKEN = msg =>
`Authorization Token is invalid: ${msg}`
@@ -1,6 +1,7 @@
import Personal from 'web3-eth-personal'

import isRequired from '../utils/isRequired'
import { UNABLE_TO_SIGN_TOKEN } from '../constants/error'

class Auth {
constructor(
@@ -34,13 +35,16 @@ class Auth {
* @param {string} userAddress - Address of the user profile
* @returns {string} Signed token for future use.
*/
validateNewAuthToken = async userAddress => {
getNewAuthToken = async userAddress => {
const unsignedToken = (await this._StoreProviderInstance.newAuthToken(
userAddress
)).unsignedToken

const signedToken = await this.signMessage(userAddress, unsignedToken)
this.setAuthToken(signedToken)
// make sure token is valid
if (!await this.validateAuthToken(userAddress, signedToken))
throw new Error(UNABLE_TO_SIGN_TOKEN)

return signedToken
}

@@ -60,6 +64,15 @@ class Auth {
})
})
}

/**
* Validate an auth token.
* @param {string} userAddress - The address of the user.
* @param {string} authToken - Token to check.
* @returns {Promise} resolves to True if token is valid.
*/
validateAuthToken = (userAddress, authToken) =>
this._StoreProviderInstance.isTokenValid(userAddress, authToken)
}

export default Auth
@@ -51,6 +51,10 @@ class StoreProviderWrapper {
body = JSON.parse(httpRequest.responseText)
// eslint-disable-next-line no-unused-vars
} catch (err) {}
// auth token error
if (httpRequest.status === 401)
reject(errorConstants.INVALID_AUTH_TOKEN(body.error))

resolve({
body: body,
status: httpRequest.status
@@ -59,7 +63,7 @@ class StoreProviderWrapper {
}
httpRequest.send(body)
} catch (err) {
reject(err)
reject(errorConstants.REQUEST_FAILED(err))
}
})
}
@@ -110,6 +114,29 @@ class StoreProviderWrapper {
return newTokenResponse.body
}

/**
* Validate auth token
* @param {string} userAddress - Address of user profile.
* @param {string} token - <optional> token to use. Sets token.
* @returns {bool} - True if token is valid.
*/
isTokenValid = async (userAddress, token) => {
if (token) this.setAuthToken(token)

try {
const response = await this._makeRequest(
'POST',
`${this._storeUri}/${userAddress}/authToken/verify`,
JSON.stringify({})
)

return response.status === 201
// eslint-disable-next-line no-unused-vars
} catch (err) {
return false
}
}

// **************************** //
// * Read * //
// **************************** //
@@ -36,7 +36,8 @@ describe('Auth', () => {
'0x7b2276657273696f6e223a312c2265787069726174696f6e223a313532353830303831313932307d'
const mockStoreProvider = {
newAuthToken: () => ({ unsignedToken: mockToken }),
setAuthToken: () => true
setAuthToken: () => true,
isTokenValid: () => true
}
// set new store provider
klerosInstance.auth.setStoreProviderInstance(mockStoreProvider)
@@ -51,7 +52,7 @@ describe('Auth', () => {
})
})

const signedToken = await klerosInstance.auth.validateNewAuthToken(
const signedToken = await klerosInstance.auth.getNewAuthToken(
loggedInUserAddress
)

@@ -19,7 +19,7 @@ describe('Arbitrator', () => {
disputeId: '1'
}
const mockStoreProvider = {
getDisputesForUser: mockGetDisputesForUser.mockReturnValue(
getDisputes: mockGetDisputesForUser.mockReturnValue(
_asyncMockResponse([mockDispute])
),
setUpUserProfile: mockShouldNotCall
@@ -51,7 +51,7 @@ describe('Arbitrator', () => {
disputeId: '1'
}
const mockStoreProvider = {
getDisputesForUser: mockGetDisputesForUser.mockReturnValue(
getDisputes: mockGetDisputesForUser.mockReturnValue(
_asyncMockResponse([mockDispute])
),
setUpUserProfile: mockSetUpUserProfile.mockReturnValue(
@@ -91,7 +91,7 @@ describe('Arbitrator', () => {
appealDraws: [1]
}
const mockStoreProvider = {
getDisputesForUser: mockGetDisputesForUser.mockReturnValue(
getDisputes: mockGetDisputesForUser.mockReturnValue(
_asyncMockResponse([mockDispute])
),
setUpUserProfile: mockSetUpUserProfile.mockReturnValue(
@@ -178,7 +178,7 @@ describe('Disputes', () => {
}
const mockStoreProvider = {
getContractByAddress: jest.fn().mockReturnValue(mockContract),
getDisputeData: jest.fn().mockReturnValue(mockUserData)
getDisputeDataForUser: jest.fn().mockReturnValue(mockUserData)
}
disputesInstance.setStoreProviderInstance(mockStoreProvider)

@@ -293,7 +293,7 @@ describe('Disputes', () => {
}
const mockStoreProvider = {
getContractByAddress: jest.fn().mockReturnValue(mockContract),
getDisputeData: jest.fn().mockReturnValue(mockUserData)
getDisputeDataForUser: jest.fn().mockReturnValue(mockUserData)
}
disputesInstance.setStoreProviderInstance(mockStoreProvider)

@@ -410,7 +410,7 @@ describe('Disputes', () => {
}
const mockStoreProvider = {
getContractByAddress: jest.fn().mockReturnValue(mockContract),
getDisputeData: jest.fn().mockReturnValue(mockUserData)
getDisputeDataForUser: jest.fn().mockReturnValue(mockUserData)
}
disputesInstance.setStoreProviderInstance(mockStoreProvider)

0 comments on commit c0bfb62

Please sign in to comment.
You can’t perform that action at this time.