All files / src verifier.js

12.9% Statements 4/31
0% Branches 0/23
0% Functions 0/8
12.9% Lines 4/31
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 751x 1x 1x   1x                                                                                                                                            
import makeDebug from 'debug'
import _ from 'lodash'
import { Verifier } from '@feathersjs/authentication-oauth2'
 
const debug = makeDebug('feathers-authentication-oauth2:verify')
 
class OAuth2Verifier extends Verifier {
  constructor (app, options = {}) {
    options.emailField = options.emailField || 'email'
    options.emailFieldInProfile = options.emailFieldInProfile || 'emails[0].value'
    super(app, options)
  }
 
  _updateEntity (entity, data) {
    const options = this.options
    const name = options.name
    const id = entity[this.service.id]
    debug(`Patching ${options.entity}: ${id}`)
 
    const newData = {
      [options.idField]: data.profile.id,
      [name]: data
    }
 
    return this.service.patch(id, newData)
  }
 
  verify (req, accessToken, refreshToken, profile, done) {
    debug('Checking credentials')
    const options = this.options
    const query = {
      $or: [
        { [options.idField]: profile.id },
        { [options.emailField]: _.get(profile, options.emailFieldInProfile) }
      ],
      $limit: 1
    }
    const data = { profile, accessToken, refreshToken }
    let existing
 
    // Check request object for an existing entity
    if (req && req[options.entity]) {
      existing = req[options.entity]
    }
 
    // Check the request that came from a hook for an existing entity
    if (!existing && req && req.params && req.params[options.entity]) {
      existing = req.params[options.entity]
    }
 
    // If there is already an entity on the request object (ie. they are
    // already authenticated) attach the profile to the existing entity
    // because they are likely "linking" social accounts/profiles.
    if (existing) {
      return this._updateEntity(existing, data)
        .then(entity => done(null, entity))
        .catch(error => error ? done(error) : done(null, error))
    }
 
    // Find or create the user since they could have signed up via facebook.
    this.service
      .find({ query })
      .then(this._normalizeResult)
      .then(entity => entity ? this._updateEntity(entity, data) : this._createEntity(data))
      .then(entity => {
        const id = entity[this.service.id]
        const payload = { [`${this.options.entity}Id`]: id }
        done(null, entity, payload)
      })
      .catch(error => error ? done(error) : done(null, error))
  }
}
 
export default OAuth2Verifier