Skip to content

Commit

Permalink
Merge pull request #17 from joehand/auth
Browse files Browse the repository at this point in the history
Add auth
  • Loading branch information
Karissa authored Dec 9, 2016
2 parents 03f4d41 + a1d81dd commit f6964df
Show file tree
Hide file tree
Showing 11 changed files with 367 additions and 6 deletions.
6 changes: 5 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ var config = {
require('../lib/commands/pull'),
require('../lib/commands/snapshot'),
require('../lib/commands/sync'),
require('../lib/commands/doctor')
require('../lib/commands/doctor'),
require('../lib/commands/auth/register'),
require('../lib/commands/auth/whoami'),
require('../lib/commands/auth/logout'),
require('../lib/commands/auth/login')
]
}

Expand Down
37 changes: 37 additions & 0 deletions lib/commands/auth/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var ui = require('../../ui')
var TownshipClient = require('../../township')

module.exports = {
name: 'login',
command: login,
options: [
{
name: 'config',
boolean: false,
default: '.datrc'
}
]
}

function login (opts) {
var email = opts.email
if (!email) ui.exitErr(new Error('Email is required.'))
var password = opts.password
if (!password) ui.exitErr(new Error('Password is required.'))

var client = TownshipClient({
server: opts.server,
config: {
filename: opts.config
}
})

client.login({
email: email,
password: password
}, function (err) {
if (err) ui.exitErr(err)
process.stdout.write('Logged in successfully.')
ui.exit()
})
}
36 changes: 36 additions & 0 deletions lib/commands/auth/logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var ui = require('../../ui')
var TownshipClient = require('../../township')

module.exports = {
name: 'logout',
command: logout,
options: [
{
name: 'config',
boolean: false,
default: '.datrc'
}
]
}

function logout (opts) {
var client = TownshipClient({
server: opts.server,
config: {
filename: opts.config
}
})

var config = client.config.get()
var login = config.currentLogin
if (!login) {
process.stdout.write('Not logged in.')
ui.exit()
} else {
client.logout(function (err) {
if (err) ui.exitErr(err)
process.stdout.write('Logged out.')
ui.exit()
})
}
}
37 changes: 37 additions & 0 deletions lib/commands/auth/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var ui = require('../../ui')
var TownshipClient = require('../../township')

module.exports = {
name: 'register',
command: register,
options: [
{
name: 'config',
boolean: false,
default: '.datrc'
}
]
}

function register (opts) {
var email = opts.email
if (!email) ui.exitErr(new Error('Email is required.'))
var password = opts.password
if (!password) ui.exitErr(new Error('Password is required.'))

var client = TownshipClient({
server: opts.server,
config: {
filename: opts.config
}
})

client.register({
email: email,
password: password
}, function (err) {
if (err) ui.exitErr(err)
process.stdout.write('Registered successfully.')
ui.exit()
})
}
28 changes: 28 additions & 0 deletions lib/commands/auth/whoami.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var ui = require('../../ui')
var TownshipClient = require('../../township')

module.exports = {
name: 'whoami',
command: whoami,
options: [
{
name: 'config',
boolean: false,
default: '.datrc'
}
]
}

function whoami (opts) {
var client = TownshipClient({
server: opts.server,
config: {
filename: opts.config
}
})
var config = client.config.get()
var login = config.currentLogin
if (!login) process.stdout.write('Not logged in.')
else process.stdout.write(login.email)
ui.exit()
}
13 changes: 13 additions & 0 deletions lib/township.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var xtend = require('xtend')
var TownshipClient = require('township-client')

module.exports = function (opts) {
var defaults = {
server: 'https://auth.dat.land',
config: {
filename: '.datrc'
}
}
var options = xtend(defaults, opts)
return TownshipClient(options)
}
8 changes: 5 additions & 3 deletions lib/usage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module.exports = function () {
console.error('Usage: dat-next <cmd> [options]')
console.error(' dat-next clone <dat-link> <directory> clone a remote dat')
console.error(' dat-next create create a local dat')
console.error(' dat-next pull download updated files from a remote dat')
console.error(' dat-next sync sync latest files with the network & import new files')
console.error(' dat-next snapshot create a local dat snapshot')
console.error(' dat-next sync sync latest files with the network. archive owners import updated files to share.')
console.error(' dat-next clone <dat-link> <directory> clone a remote dat')
console.error(' dat-next pull download updated files from a remote dat and exit')
console.error(' dat-next doctor run the dat network doctor')
console.error(' dat-next register register for an account')
console.error(' dat-next login login to an account')
console.error('')
console.error(' --dir=<folder> set directory (all commands)')
console.error(' --no-import do not import files (create, sync)')
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@
"pretty-bytes": "^4.0.2",
"progress-string": "^1.2.1",
"status-logger": "^3.0.0",
"subcommand": "^2.0.4"
"subcommand": "^2.0.4",
"township-client": "^1.1.0",
"xtend": "^4.0.1"
},
"devDependencies": {
"appa": "^5.0.0",
"homedir": "^0.6.0",
"memdb": "^1.3.1",
"mkdirp": "^0.5.1",
"recursive-readdir-sync": "^1.0.6",
"rimraf": "^2.5.4",
"standard": "^8.6.0",
"tap-spec": "^4.1.1",
"tape": "^4.6.3",
"tape-spawn": "^1.4.2"
"tape-spawn": "^1.4.2",
"township": "^1.1.0"
}
}
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ dat-next sync [--dir=<folder>]
```

Download latest files and keep connection open to continue updating as remote source is updated.

## Auth (experimental)

```
dat-verb logout
dat-verb login
dat-verb register
dat-verb whoami
```

All take `server` option along with `username` and `password`.
154 changes: 154 additions & 0 deletions tests/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
var test = require('tape')
var path = require('path')
var fs = require('fs')
var homedir = require('homedir')
var spawn = require('./helpers/spawn')
var help = require('./helpers')
var authServer = require('./helpers/auth-server')

var dat = path.resolve(path.join(__dirname, '..', 'bin', 'cli.js'))
var baseTestDir = help.testFolder()

var port = process.env.PORT || 3000
var SERVER = 'http://localhost:' + port
var config = '.datrc-test'
var opts = ' --server=' + SERVER + ' --config=' + config

dat += opts

authServer(port, function (server) {
test('auth - whoami works when not logged in', function (t) {
var cmd = dat + ' whoami '
var st = spawn(t, cmd, {cwd: baseTestDir})
st.stdout.match(function (output) {
t.same('Not logged in.', output, 'printed correct output')
return true
})
st.stderr.empty()
st.end()
})

test('auth - register requires email', function (t) {
var cmd = dat + ' register '
var st = spawn(t, cmd, {cwd: baseTestDir})
st.stderr.match(function (output) {
t.ok(output.indexOf('Email is required') > -1, 'outputs correct error message')
st.kill()
return true
})
st.stdout.empty()
st.fails()
st.end()
})

test('auth - register requires email and password', function (t) {
var cmd = dat + ' register [email protected]'
var st = spawn(t, cmd, {cwd: baseTestDir})
st.stderr.match(function (output) {
t.ok(output.indexOf('Password is required') > -1, 'outputs correct error message')
st.kill()
return true
})
st.stdout.empty()
st.fails()
st.end()
})

test('auth - register works', function (t) {
var cmd = dat + ' register [email protected] --password=joe'
var st = spawn(t, cmd, {cwd: baseTestDir})
st.stdout.match(function (output) {
t.same(output.trim(), 'Registered successfully.', 'output success message')
return true
})
st.stderr.empty()
st.end()
})

test('auth - login requires email', function (t) {
var cmd = dat + ' login --password=joe'
var st = spawn(t, cmd, {cwd: baseTestDir})
st.stderr.match(function (output) {
t.ok(output.indexOf('Email is required') > -1, 'outputs correct error message')
st.kill()
return true
})
st.stdout.empty()
st.end()
})

test('auth - login requires email and password', function (t) {
var cmd = dat + ' login [email protected]'
var st = spawn(t, cmd, {cwd: baseTestDir})
st.stderr.match(function (output) {
t.ok(output.indexOf('Password is required') > -1, 'outputs correct error message')
st.kill()
return true
})
st.stdout.empty()
st.end()
})

test('auth - login works', function (t) {
var cmd = dat + ' login [email protected] --password=joe'
var st = spawn(t, cmd, {cwd: baseTestDir})
st.stdout.match(function (output) {
t.same(output.trim(), 'Logged in successfully.', 'output success message')
return true
})
st.stderr.empty()
st.end()
})

test('auth - whoami works', function (t) {
var cmd = dat + ' whoami'
var st = spawn(t, cmd, {cwd: baseTestDir})
st.stdout.match(function (output) {
t.same('[email protected]', output, 'email printed')
return true
})
st.stderr.empty()
st.end()
})

test('auth - logout works', function (t) {
var cmd = dat + ' logout'
var st = spawn(t, cmd, {cwd: baseTestDir})
st.stdout.match(function (output) {
t.same('Logged out.', output, 'output correct')
return true
})
st.stderr.empty()
st.end()
})

test('auth - logout prints correctly when trying to log out twice', function (t) {
var cmd = dat + ' logout'
var st = spawn(t, cmd, {cwd: baseTestDir})
st.stdout.match(function (output) {
t.same('Not logged in.', output, 'output correct')
return true
})
st.stderr.empty()
st.end()
})

test('auth - whoami works after logging out', function (t) {
var cmd = dat + ' whoami'
var st = spawn(t, cmd, {cwd: baseTestDir})
st.stdout.match(function (output) {
t.same('Not logged in.', output)
return true
})
st.stderr.empty()
st.end()
})

test.onFinish(function () {
server.close(function () {
fs.unlink(path.join(homedir(), config), function () {
// done!
})
})
})
})
Loading

0 comments on commit f6964df

Please sign in to comment.