Skip to content

Commit

Permalink
fixed whoami
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Aug 28, 2017
1 parent a667ac2 commit 6a8f0f7
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 54 deletions.
34 changes: 20 additions & 14 deletions lib/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
'use strict'

const user = require('../user')
const config = require('../config')

function * doAuth (ctx, next) {
if (ctx.headers.authorization) {
let token = ctx.headers.authorization.split(' ')[1]
ctx.username = yield user.findByToken(token)
async function doAuth (req, res, next) {
if (req.headers.authorization) {
let token = req.headers.authorization.split(' ')[1]
req.username = await user.findByToken(token)
}
if (!ctx.username) ctx.throw(401)
yield next
if (!req.username) return res.status(401).end()
next()
}

module.exports = {
read: function * (next) {
yield (config.auth.read ? doAuth(this, next) : next)
read: function (req, res, next) {
if (config.auth.read) {
doAuth(req, res, next)
} else {
next()
}
},
write: function * (next) {
yield (config.auth.write ? doAuth(this, next) : next)
write: function (req, res, next) {
if (config.auth.write) {
doAuth(req, res, next)
} else {
next()
}
},
always: function * (next) {
yield doAuth(this, next)
always: function (req, res, next) {
doAuth(req, res, next)
}
}
27 changes: 12 additions & 15 deletions lib/routes/auth.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
'use strict'

const r = require('koa-router')()
const r = module.exports = require('express').Router()
const aw = require('./asyncawait.js')
const user = require('../user')
const parse = require('co-body')
const bodyParser = require('body-parser')
const middleware = require('../middleware')

r.use(bodyParser.json())

// login
r.put('/-/user/:user', function * () {
let auth = yield user.authenticate(yield parse(this))
r.put('/-/user/:user', aw(async function (req, res) {
let auth = await user.authenticate(req.body)
if (auth) {
this.status = 201
this.body = {token: auth}
res.status(201).json({token: auth})
} else {
this.status = 401
this.body = {error: 'invalid credentials'}
res.status(401).json({error: 'invalid credentials'})
}
})
}))

// whoami
r.get('/-/whoami', middleware.auth.always, function * () {
this.body = {username: this.username}
r.get('/-/whoami', middleware.auth.always, (req, res) => {
res.status(200).json({username: req.username})
})

module.exports = r
2 changes: 1 addition & 1 deletion lib/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ let r = module.exports = require('express').Router()

r.use(require('./ping'))
r.use(require('./version'))
r.use(require('./auth'))
r.use(require('./packages'))
// load('tarballs')
// load('auth')
// load('publish')
// load('dist_tags')
2 changes: 0 additions & 2 deletions lib/routes/publish.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

const r = require('koa-router')()
const crypto = require('crypto')
const parse = require('co-body')
Expand Down
37 changes: 18 additions & 19 deletions lib/user.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
'use strict'

const htpasswd = require('htpasswd-auth')
const uuid = require('node-uuid')
const config = require('./config')

function * getCreds () {
return yield JSON.parse((yield config.storage.get('auth_tokens')) || '{}')
async function getCreds () {
console.dir((await config.storage.getJSON('auth_tokens')) || {})
return (await config.storage.getJSON('auth_tokens')) || {}
}

function * createAuthToken (username) {
let creds = yield getCreds()
async function createAuthToken (username) {
let creds = await getCreds()
let token = uuid.v4()
creds[token] = {
username,
timestamp: new Date()
}
yield config.storage.put('auth_tokens', creds, {
await config.storage.put('auth_tokens', creds, {
'Content-Type': 'application/json'
})
return token
}

function * authenticate (user) {
let creds = (yield config.storage.get('htpasswd')) || ''
let auth = yield htpasswd.authenticate(user.name, user.password, creds.toString())
if (!auth) return false
return yield createAuthToken(user.name)
}
class Auth {
static async authenticate (user) {
let creds = (await config.storage.get('htpasswd')) || ''
let auth = await htpasswd.authenticate(user.name, user.password, creds.toString())
if (!auth) return false
return createAuthToken(user.name)
}

function * findByToken (token) {
let creds = yield getCreds()
if (creds[token]) return creds[token].username
static async findByToken (token) {
let creds = await getCreds()
if (creds[token]) return creds[token].username
}
}

exports.authenticate = authenticate
exports.findByToken = findByToken
module.exports = Auth
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dependencies": {
"aws-sdk": "^2.104.0",
"bluebird": "^3.5.0",
"body-parser": "^1.17.2",
"compression": "^1.7.0",
"express": "^4.15.4",
"fs-extra": "^4.0.1",
Expand Down
47 changes: 44 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ bluebird@^3.5.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.0.tgz#791420d7f551eea2897453a8a77653f96606d67c"

body-parser@^1.17.2:
version "1.17.2"
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.17.2.tgz#f8892abc8f9e627d42aedafbca66bf5ab99104ee"
dependencies:
bytes "2.4.0"
content-type "~1.0.2"
debug "2.6.7"
depd "~1.1.0"
http-errors "~1.6.1"
iconv-lite "0.4.15"
on-finished "~2.3.0"
qs "6.4.0"
raw-body "~2.2.0"
type-is "~1.6.15"

brace-expansion@^1.0.0:
version "1.1.6"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.6.tgz#7197d7eaa9b87e648390ea61fc66c84427420df9"
Expand Down Expand Up @@ -339,6 +354,10 @@ builtin-modules@^1.0.0, builtin-modules@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f"

bytes@2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-2.4.0.tgz#7d97196f9d5baf7f6935e25985549edd2a6c2339"

bytes@2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-2.5.0.tgz#4c9423ea2d252c270c41b2bdefeff9bb6b62c06a"
Expand Down Expand Up @@ -555,6 +574,12 @@ debug-log@^1.0.0, debug-log@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debug-log/-/debug-log-1.0.1.tgz#2307632d4c04382b8df8a32f70b895046d52745f"

debug@2.6.7:
version "2.6.7"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.7.tgz#92bad1f6d05bbb6bba22cca88bcd0ec894c2861e"
dependencies:
ms "2.0.0"

debug@2.6.8, debug@^2.6.3, debug@^2.6.6, debug@^2.6.8:
version "2.6.8"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc"
Expand Down Expand Up @@ -629,7 +654,7 @@ denque@^1.1.0:
version "1.2.2"
resolved "https://registry.yarnpkg.com/denque/-/denque-1.2.2.tgz#e06cf7cf0da8badc88cbdaabf8fc0a70d659f1d4"

depd@1.1.1, depd@~1.1.1:
depd@1.1.1, depd@~1.1.0, depd@~1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.1.tgz#5783b4e1c459f06fa5ca27f991f3d06e7a310359"

Expand Down Expand Up @@ -1302,7 +1327,7 @@ http-call@^2.1.5:
is-stream "^1.1.0"
tunnel-agent "^0.6.0"

http-errors@~1.6.2:
http-errors@~1.6.1, http-errors@~1.6.2:
version "1.6.2"
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.6.2.tgz#0a002cc85707192a7e7946ceedc11155f60ec736"
dependencies:
Expand All @@ -1315,6 +1340,10 @@ https-pem@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/https-pem/-/https-pem-1.0.4.tgz#f34d986916d051563a7fc6ba96ddb1c28634dc18"

iconv-lite@0.4.15:
version "0.4.15"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.15.tgz#fe265a218ac6a57cfe854927e9d04c19825eddeb"

ieee754@^1.1.4:
version "1.1.8"
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.8.tgz#be33d40ac10ef1926701f6f08a2d86fbfd1ad3e4"
Expand Down Expand Up @@ -2376,6 +2405,10 @@ punycode@1.3.2:
version "1.3.2"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d"

qs@6.4.0:
version "6.4.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.4.0.tgz#13e26d28ad6b0ffaa91312cd3bf708ed351e7233"

qs@6.5.0:
version "6.5.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.0.tgz#8d04954d364def3efc55b5a0793e1e2c8b1e6e49"
Expand All @@ -2399,6 +2432,14 @@ range-parser@~1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.0.tgz#f49be6b487894ddc40dcc94a322f611092e00d5e"

raw-body@~2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.2.0.tgz#994976cf6a5096a41162840492f0bdc5d6e7fb96"
dependencies:
bytes "2.4.0"
iconv-lite "0.4.15"
unpipe "1.0.0"

read-pkg-up@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-1.0.1.tgz#9d63c13276c065918d57f002a57f40a1b643fb02"
Expand Down Expand Up @@ -2978,7 +3019,7 @@ unix-crypt-td-js@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unix-crypt-td-js/-/unix-crypt-td-js-1.0.0.tgz#1c0824150481bc7a01d49e98f1ec668d82412f3b"

unpipe@~1.0.0:
unpipe@1.0.0, unpipe@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"

Expand Down

0 comments on commit 6a8f0f7

Please sign in to comment.