]> gerrit.simantics Code Review - simantics/district.git/blob - whoami.js
68db49e59a402a702df40e067abac3a351489689
[simantics/district.git] / whoami.js
1 module.exports = whoami
2
3 var url = require('url')
4 var assert = require('assert')
5
6 function whoami (uri, params, cb) {
7   assert(typeof uri === 'string', 'must pass registry URI to whoami')
8   assert(params && typeof params === 'object', 'must pass params to whoami')
9   assert(typeof cb === 'function', 'must pass callback to whoami')
10
11   var auth = params.auth
12   assert(auth && typeof auth === 'object', 'must pass auth to whoami')
13
14   if (auth.username) return process.nextTick(cb.bind(this, null, auth.username))
15
16   this.request(url.resolve(uri, '-/whoami'), { auth: auth }, function (er, userdata) {
17     if (er) return cb(er)
18
19     cb(null, userdata.username)
20   })
21 }