-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor proof verification and torusVerifier, add facebookVerifier, …
…add email verification by accessToken
- Loading branch information
1 parent
3f6d72a
commit 6defbdf
Showing
8 changed files
with
225 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// @flow | ||
|
||
import Axios from 'axios' | ||
import { isPlainObject } from 'lodash' | ||
|
||
import Config from '../server/server.config' | ||
|
||
class FacebookVerifier { | ||
constructor(Config, httpFactory) { | ||
const { facebookGraphApiUrl } = Config | ||
|
||
this.http = httpFactory({ | ||
baseURL: facebookGraphApiUrl, | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
|
||
this._configureResponses() | ||
} | ||
|
||
async verifyEmail(userEmail, accessToken) { | ||
const params = { fields: 'email', access_token: accessToken } | ||
const userInfo = await this.http.get('/me', { params }) | ||
|
||
if (!('email' in userInfo)) { | ||
throw new Error( | ||
"Couldn't verify email: user hasn't confirmed it on Facebook " + 'or has used mobile phone number for sign in.' | ||
) | ||
} | ||
|
||
return userEmail === userInfo.email | ||
} | ||
|
||
_configureResponses() { | ||
const { response } = this.http.interceptors | ||
|
||
response.use(response => this._transformResponse(response), exception => this._exceptionInterceptor(exception)) | ||
} | ||
|
||
_transformResponse(response) { | ||
return response.data | ||
} | ||
|
||
_exceptionInterceptor(exception) { | ||
const { response, message } = exception | ||
|
||
if (response && isPlainObject(response.data)) { | ||
const graphResponse = this._transformResponse(response) | ||
const { message: graphMessage } = graphResponse.error || {} | ||
|
||
exception.message = graphMessage || message | ||
exception.response = graphResponse | ||
} else { | ||
delete exception.response | ||
} | ||
|
||
throw exception | ||
} | ||
} | ||
|
||
export default new FacebookVerifier(Config, Axios.create) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { assign } from 'lodash' | ||
|
||
import TorusVerifier from '../../imports/torusVerifier' | ||
import FacebookVerifier from '../../imports/facebookVerifier' | ||
|
||
class UserVerifier { | ||
constructor(userRecord, requestPayload, logger) { | ||
assign(this, { userRecord, requestPayload, logger }) | ||
} | ||
|
||
async verifyEmail(email, torusAccessToken) { | ||
let emailVerified = false | ||
const { userRecord, logger } = this | ||
|
||
try { | ||
emailVerified = await FacebookVerifier.verifyEmail(email, torusAccessToken) | ||
} catch (exception) { | ||
const { message: msg } = exception | ||
|
||
logger.warn('FacebookVerifier failed:', { e: exception, msg }) | ||
} | ||
|
||
logger.info('FacebookVerifier result:', { emailVerified }) | ||
userRecord.isEmailConfirmed |= emailVerified | ||
} | ||
|
||
async verifyProof(torusProof, torusProvider, torusProofNonce) { | ||
const { userRecord, requestPayload, logger } = this | ||
let verificationResult = { emailVerified: false, mobileVerified: false } | ||
|
||
try { | ||
verificationResult = await TorusVerifier.verifyProof(torusProof, torusProvider, requestPayload, torusProofNonce) | ||
} catch (exception) { | ||
const { message: msg } = exception | ||
|
||
logger.warn('TorusVerifier failed:', { e: exception, msg }) | ||
} | ||
|
||
const { emailVerified, mobileVerified } = verificationResult | ||
|
||
logger.info('TorusVerifier result:', verificationResult) | ||
userRecord.smsValidated |= mobileVerified | ||
userRecord.isEmailConfirmed |= emailVerified | ||
} | ||
} | ||
|
||
export default (userRecord, requestPayload, logger) => new UserVerifier((userRecord, requestPayload, logger)) |