Skip to content

Commit 4ad56fb

Browse files
committed
change import to just https import
Organizes utility classes to declutter Client module
1 parent fd964c2 commit 4ad56fb

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/simperium/auth.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
// @flow
22
import events from 'events'
3-
import https from 'https'
3+
import { request } from 'https'
44
import url from 'url'
55

66
// @flow
7-
type User = {};
7+
type User = {
8+
options: {},
9+
access_token: string,
10+
};
811

9-
const fromJSON = ( json: string ) => {
10-
const data = JSON.parse( json );
12+
const fromJSON = ( json: string ): User => {
13+
const data: {} = JSON.parse( json );
14+
if ( ! data.access_token && typeof data.access_token !== 'string' ) {
15+
throw new Error( 'access_token not present' );
16+
}
1117
return {
1218
options: data,
13-
access_token: data.access_token
19+
access_token: new String( data.access_token ).toString()
1420
};
1521
};
1622

@@ -27,16 +33,32 @@ export class AuthError extends Error {
2733
}
2834
}
2935

36+
/**
37+
* Client for creating and authenticating Simperium.com user accounts.
38+
*/
3039
export class Auth extends EventEmitter {
3140
appId: string
3241
appSecret: string
3342

43+
/**
44+
* Creates an instance of the Auth client
45+
*
46+
* @param {string} appId - Simperium.com application ID
47+
* @param {string} appSecret - Simperium.com application secret
48+
*/
3449
constructor( appId: string, appSecret: string ) {
3550
super();
3651
this.appId = appId;
3752
this.appSecret = appSecret;
3853
}
3954

55+
/**
56+
* Authorizes a user account with username and password
57+
*
58+
* @param {string} username account username
59+
* @param {string} password account password
60+
* @returns {Promise<User>} user account data
61+
*/
4062
authorize( username: string, password: string ) {
4163
const body = JSON.stringify( { username: username, password: password } );
4264
return this.request( 'authorize/', body );
@@ -62,7 +84,7 @@ export class Auth extends EventEmitter {
6284

6385
request( endpoint: string, body: string ): Promise<User> {
6486
return new Promise( ( resolve, reject ) => {
65-
const req = https.request( this.getUrlOptions( endpoint ), ( res ) => {
87+
const req = request( this.getUrlOptions( endpoint ), ( res ) => {
6688
let responseData = '';
6789

6890
res.on( 'data', ( data ) => {

test/simperium/auth_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ describe( 'Auth', () => {
5151
} );
5252
} );
5353

54+
it( 'should fail if missing access_token', () => {
55+
stubResponse( '{"hello":"world"}' );
56+
return auth.authorize( 'username', 'password' )
57+
.catch( error => {
58+
equal( error.message, 'Failed to authenticate user.' );
59+
equal( error.underlyingError.message, 'access_token not present' );
60+
} );
61+
} );
62+
5463
it( 'should fail to auth with invalid credentials', () => {
5564
stubResponse( 'this is not json' )
5665

0 commit comments

Comments
 (0)