Thu Jan 12 2017
Copied to clipboard! Copy reply
  • 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
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
/*
 * Handle authentication things like logging in,
 * out, etc. Our login system is silly simple -
 * when user logs in, send key to to client to
 * store in localstorage. Fingers crossed no
 * one internally will session hijack.
 */

import fs from 'fs-extra'
import ldap from 'ldapjs'
import cfg from '../config'


export default class Auth
{

  static login(ctx, next)
  {

    // I should definitely be catching errors here but f it
    const data = JSON.parse(ctx.request.body)

    return new Promise( (resolve, reject) => {
      var client = ldap.createClient({ url: cfg.ldapServer })
      client.on('error', err => {
        ctx.body = {result: 0, payload: 'LDAP server not found'}
        resolve(next())
      })

      client.on('connect', s => {
        client.bind( `ROSETTASTONE\\${data.username}`, `${data.passwd}`, err => {
          if (err)
          {
            ctx.body = {result: 0, payload: 'Invalid Credentials.'}
          }
          else
          {
            ctx.body = {result: 1, payload: 'success'}
          }
          // Set time out just for some loading icon swag
          setTimeout(function() {
            resolve(next())
          }, 2000)
        })
      })
    })
  }


  static logout(ctx, next)
  {
    console.log('log me out')
  }

}









/*
 * In the future, if you need more ldap info from a certain
 * user, use the code below. It should go after the first
 * bind (where user auths with user/pw)
  opts = {
    filter: '(samAccountName=bfranchi)',
    scope: 'sub'
  }


  client.search('dc=rosettastone,dc=local', opts, (err, res) =>
  {
    res.on('searchEntry', entry =>
    {
      console.log(entry.Object)
    })

    res.on('error', err =>
    {
      // TODO. handle client/tcp error here
    })
  })


  command line:
  ldapsearch -h 10.130.250.253 -D bfranchi@rosettastone.local -bdc=rosettastone,dc=local  -W "(samAccountName=bfranchi)
  */