Skip to content

Commit

Permalink
Implement error classes, see #16
Browse files Browse the repository at this point in the history
  • Loading branch information
David Kosub committed Aug 22, 2014
1 parent 92b47eb commit 2bbc4de
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/errors.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.ConsumerError = class ConsumerSecretError extends Error
exports.StoreError = class MissingStoreError extends Error
exports.ParameterError = class ParameterError extends Error
exports.SignatureError = class SignatureError extends Error
exports.NonceError = class NonceError extends Error
1 change: 1 addition & 0 deletions src/ims-lti.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exports = module.exports =
# Provider and Consumer classes
Provider: require './provider'
Consumer: require './consumer'
Errors: require './errors'

Stores:
RedisStore: require './redis-nonce-store'
Expand Down
13 changes: 7 additions & 6 deletions src/provider.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
HMAC_SHA1 = require './hmac-sha1'
MemoryNonceStore = require './memory-nonce-store'
errors = require './errors'



Expand All @@ -11,16 +12,16 @@ class Provider
constructor: (consumer_key, consumer_secret, nonceStore, signature_method=(new HMAC_SHA1()) ) ->

if typeof consumer_key is 'undefined' or consumer_key is null
throw new Error 'Must specify consumer_key'
throw new errors.ConsumerError 'Must specify consumer_key'

if typeof consumer_secret is 'undefined' or consumer_secret is null
throw new Error 'Must specify consumer_secret'
throw new errors.ConsumerError 'Must specify consumer_secret'

if not nonceStore
nonceStore = new MemoryNonceStore(consumer_key)

if not nonceStore.isNonceStore?()
throw new Error 'Fourth argument must be a nonceStore object'
throw new errors.ParameterError 'Fourth argument must be a nonceStore object'

@consumer_key = consumer_key
@consumer_secret = consumer_secret
Expand All @@ -35,7 +36,7 @@ class Provider
valid_request: (req, callback=()->) =>
@parse_request(req)
if not @_valid_parameters(req)
return callback(new Error('Invalid LTI parameters'), false)
return callback(new errors.ParameterError('Invalid LTI parameters'), false)
@_valid_oauth req, (err, valid) -> callback err, valid


Expand All @@ -55,10 +56,10 @@ class Provider
_valid_oauth: (req, callback) ->
generated = @signer.build_signature req, @consumer_secret
valid_signature = generated is req.body.oauth_signature
return callback new Error('Invalid Signature'), false if not valid_signature
return callback new errors.SignatureError('Invalid Signature'), false if not valid_signature
@nonceStore.isNew req.body.oauth_nonce, req.body.oauth_timestamp, (err, valid) ->
if not valid
callback new Error('Expired nonce'), false
callback new errors.NonceError('Expired nonce'), false
else
callback null, true

Expand Down

0 comments on commit 2bbc4de

Please sign in to comment.