Skip to content

Commit

Permalink
author model
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Aug 29, 2010
1 parent 500a6e7 commit 7ca9b27
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ Showoff Time is a hosted presentation server heavily inspired by Showoff.
* `/preview` - render the current data and return it
* `/live/:name#s1234` - view a live talk.

## Model

### Presentation

* author (ID)
* title (String)
* published (boolean, default: false)

### Slide

* presentation (ID)
* number (Integer)
* body (String)
* options (Array, see Slide.validOptions)

### Author

* name
* email
* password

## Development

Run this in the main directory to compile coffeescript to javascript as you go:
Expand Down
35 changes: 35 additions & 0 deletions lib/author.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var Author, crypto;
crypto = require('crypto');
Author = function(options) {
this.fill(options);
this.db = options.db || Author.db;
return this;
};
Author.prototype.fill = function(options) {
this.name = options.name;
this.email = options.email;
this.salt = options.salt || Author.generate_salt();
return (this.pw = options.pw);
};
Author.prototype.set_password = function(pass) {
return (this.pw = Author.hash_password(this.salt, pass));
};
Author.prototype.authenticates = function(pass) {
return Author.hash_password(this.salt, pass) === this.pw;
};
Author.generate_salt = function() {
var hash;
hash = crypto.createHash('md5');
hash.update(this.name);
hash.update(this.email);
hash.update(Math.random().toString());
return hash.digest('hex');
};
Author.hash_password = function(salt, pass) {
var hash;
hash = crypto.createHash('md5');
hash.update(salt);
hash.update(pass);
return hash.digest('hex');
};
exports.Author = Author;
33 changes: 33 additions & 0 deletions src/author.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
crypto = require('crypto')

class Author
constructor: (options) ->
@fill options
@db = options.db || Author.db

fill: (options) ->
@name = options.name
@email = options.email
@salt = options.salt || Author.generate_salt()
@pw = options.pw # hashed password

set_password: (pass) ->
@pw = Author.hash_password @salt, pass

authenticates: (pass) ->
Author.hash_password(@salt, pass) == @pw

Author.generate_salt = ->
hash = crypto.createHash 'md5'
hash.update @name
hash.update @email
hash.update Math.random().toString() # shoot me
hash.digest 'hex'

Author.hash_password = (salt, pass) ->
hash = crypto.createHash 'md5'
hash.update salt
hash.update pass
hash.digest 'hex'

exports.Author = Author
12 changes: 12 additions & 0 deletions test/author_test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
assert = require 'assert'
Author = require('../lib/author').Author

author = new Author name: "Name", email: "Email"

assert.equal true, author.salt?
assert.equal false, author.pw?

author.set_password "secret"

assert.equal true, author.authenticates('secret')
assert.equal false, author.authenticates('foo')

0 comments on commit 7ca9b27

Please sign in to comment.