Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernizing and using flowtype #60

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"presets": ["es2015"]
"presets": ["es2015", "flow"],
"plugins": [ "transform-object-rest-spread" ]
}
12 changes: 12 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[ignore]

[include]
src/

[libs]

[lints]

[options]

[strict]
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"url": "git://github.com/Simperium/node-simperium.git"
},
"scripts": {
"flow": "flow",
"test": "mocha --compilers js:babel-core/register --require test/helper test/**",
"prepublish": "babel -q -d lib/ src/"
},
Expand All @@ -21,8 +22,12 @@
"babel-cli": "^6.2.0",
"babel-core": "^6.2.0",
"babel-eslint": "^8.2.1",
"babel-preset-es2015": "^6.2.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-flow": "^6.23.0",
"eslint": "^4.17.0",
"mocha": "^2.3.4"
"flow": "^0.2.3",
"flow-bin": "^0.65.0",
"mocha": "^2.5.3"
}
}
143 changes: 96 additions & 47 deletions src/simperium/auth.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,116 @@
import { EventEmitter } from 'events'
import User from './user'
import { format, inherits } from 'util'
import https from 'https'
// @flow
import events from 'events'
import { request } from 'https'
import url from 'url'

// @flow
type User = {
options: {},
access_token: string,
};

const fromJSON = ( json: string ): User => {
const data: {} = JSON.parse( json );
if ( ! data.access_token && typeof data.access_token !== 'string' ) {
throw new Error( 'access_token not present' );
}
return {
options: data,
access_token: new String( data.access_token ).toString()
};
};

const { EventEmitter } = events;

const URL = 'https://auth.simperium.com/1';

export default function Auth( appId, appSecret ) {
this.appId = appId;
this.appSecret = appSecret;
export class AuthError extends Error {
underlyingError: Error

constructor( underlyingError: Error ) {
super( 'Failed to authenticate user.' );
this.underlyingError = underlyingError;
}
}

inherits( Auth, EventEmitter );
/**
* Client for creating and authenticating Simperium.com user accounts.
*/
export class Auth extends EventEmitter {
appId: string
appSecret: string

Auth.prototype.authorize = function( username, password ) {
var body = JSON.stringify( { username: username, password: password } ),
promise = this.request( 'authorize/', body );
/**
* Creates an instance of the Auth client
*
* @param {string} appId - Simperium.com application ID
* @param {string} appSecret - Simperium.com application secret
*/
constructor( appId: string, appSecret: string ) {
super();
this.appId = appId;
this.appSecret = appSecret;
}

return promise;
}
/**
* Authorizes a user account with username and password
*
* @param {string} username account username
* @param {string} password account password
* @returns {Promise<User>} user account data
*/
authorize( username: string, password: string ) {
const body = JSON.stringify( { username: username, password: password } );
return this.request( 'authorize/', body );
}

Auth.prototype.create = function( username, password, provider ) {
var userData = { username, password };
if ( provider ) {
userData.provider = provider;
create( username: String, password: String, provider: ?String ) {
const userData: { username: String, password: String, provider?: String } = { username, password };
if ( provider ) {
userData.provider = provider;
}
const body = JSON.stringify( userData );
return this.request( 'create/', body );
}
var body = JSON.stringify( userData ),
promise = this.request( 'create/', body );

return promise;
}
getUrlOptions( path: string ) {
const options = url.parse( `${URL}/${ this.appId }/${ path}` );
return {
... options,
method: 'POST',
headers: {'X-Simperium-API-Key': this.appSecret }
};
}

Auth.prototype.getUrlOptions = function( path ) {
const options = url.parse( format( '%s/%s/%s', URL, this.appId, path ) );
return Object.assign( options, { method: 'POST', headers: {'X-Simperium-API-Key': this.appSecret}} );
}
request( endpoint: string, body: string ): Promise<User> {
return new Promise( ( resolve, reject ) => {
const req = request( this.getUrlOptions( endpoint ), ( res ) => {
let responseData = '';

Auth.prototype.request = function( endpoint, body ) {
return new Promise( ( resolve, reject ) => {
const req = https.request( this.getUrlOptions( endpoint ), ( res ) => {
var responseData = '';
res.on( 'data', ( data ) => {
responseData += data.toString();
} );

res.on( 'data', ( data ) => {
responseData += data.toString();
res.on( 'end', () => {
try {
const user = fromJSON( responseData );
resolve( user );
this.emit( 'authorize', user );
} catch ( error ) {
return reject( new AuthError( error ) );
}
} );
} );

res.on( 'end', () => {
var user;

try {
user = User.fromJSON( responseData );
} catch ( e ) {
return reject( new Error( responseData ) );
}
this.emit( 'authorize', user );
resolve( user );
req.on( 'error', ( e ) => {
reject( e );
} );
} );

req.on( 'error', ( e ) => {
reject( e );
req.end( body );
} );
}
};

req.end( body );
} );
}
export default ( appId: string, appSecret: string ) => {
return new Auth( appId, appSecret );
};
Loading