{"size": 2413, "ext": "js", "max_stars_repo_path": "src/Authorizer.js", "max_stars_repo_name": "simplygreatwork/godsend-extras", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/Authorizer.js", "max_issues_repo_name": "simplygreatwork/godsend-extras", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/Authorizer.js", "max_forks_repo_name": "simplygreatwork/godsend-extras", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.7387387387, "max_line_length": 64, "alphanum_fraction": 0.5793617903}

var godsend = require('godsend');
var basic = require('godsend-basics');
var Class = godsend.Class;
var Storage = require('./store/Storage');

Authorizer = module.exports = Class.extend({
	
	initialize: function(properties) {
		
		Object.assign(this, properties);
		this.address = this.address || basic.Utility.local();
		this.storage = new Storage({
			users: this.users
		});
	},
	
	connect: function(callback) {
		
		new godsend.Bus({
			address: this.address,
			secure: false
		}).connect({
			credentials: {
				username: Credentials.get('authenticator').username,
				passphrase: Credentials.get('authenticator').passphrase,
			},
			initialized : function(connection) {
				this.mount(connection);
			}.bind(this),
			connected: function(connection) {
				this.connection = connection;
				callback();
			}.bind(this),
			errored : function(errors) {
				console.error('Connection errors: ' + errors);
			}.bind(this)
		});
	},
	
	mount: function(connection) {
		
		connection.mount({
			id: 'authentication-get-user',
			on: function(request) {
				request.accept({
					topic: 'authentication',
					action: 'get-user'
				});
			}.bind(this),
			run: function(stream) {
				this.storage.get({
					collection: 'users',
					key: stream.object.username,
					callback: function(properties) {
						stream.push(properties.value);
						stream.next();
					}
				});
			}.bind(this)
		});

		connection.mount({
			id: 'authentication-put-user',
			on: function(request) {
				request.accept({
					topic: 'authentication',
					action: 'put-user'
				});
			}.bind(this),
			run: function(stream) {
				this.storage.get({
					collection: 'users',
					key: stream.object.username,
					callback: function(properties) {
						if (properties.value) {
							stream.error({
								message: 'The user already exists and may not be added.'
							});
							stream.next();
						} else {
							this.storage.put({
								collection: 'users',
								key: stream.object.credentials.username,
								value: stream.object,
								callback: function(properties) {
									if (properties.error) {
										stream.push({
											success: false
										});
									} else {
										stream.push({
											success: true
										});
									}
									stream.next();
								}.bind(this)
							});
						}
					}.bind(this)
				});
			}.bind(this)
		});
	},
	
	unmount : function() {
		
		
	}
});
