0.3.5 • Published 8 years ago

castleio-sdk v0.3.5

Weekly downloads
27
License
MIT
Repository
-
Last release
8 years ago

Node.js SDK for Castle

Castle adds real-time monitoring of your authentication stack, instantly notifying you and your users on potential account hijacks.

Installation

Obtain the latest version of the SDK with npm:

npm install castleio-sdk

Getting Started: The Vanilla Way

Initializing

import Castle from 'castleio-sdk';
var castle = new Castle({apiSecret : 'YOUR-SECRET-HERE'});

Tracking Events

castle.trackEvent({
    event     : Castle.Events.LOGIN_SUCCEEDED, //This can also be a string EX: $login.failed
    user_id   : 2473, //The ID of your user
    details   : { //Optional
            email: 'castle@castle.io'
    },
    userAgent : 'Really long user agent string here',
    cookie    : 'The cookie the client side javascript created with the name __cid',
    ip        : '0.0.0.0',
    headers   : {} Tons of headers here
}).then(obj => {
    //Handle success
    //Note that "obj" is almost always just {}
}).catch(e => {
    //Handle error
});

Identifying Users

castle.identify({
    user_id   : 2473, //The ID of your user
    user_data   : { //Optional
            email: 'castle@castle.io'
    },
    userAgent : 'Really long user agent string here',
    cookie    : 'The cookie the client side javascript created with the name __cid',
    ip        : '0.0.0.0',
    headers   : {} Tons of headers here
}).then(obj => {
    //Handle success
    //Note that "obj" is almost always just {}
}).catch(e => {
    //Handle error
});

Getting Started: The Express Way

This is the way to go if you're using Express 4.x

Initializing

import Castle from 'castleio-sdk'
app.use(Castle.express({apiSecret : 'YOUR-SECRET-HERE'}));

Tracking Events

(request, response, next) => {
    request.castleTrackEvent({
        event     : request.castleEvents.LOGIN_SUCCEEDED,
        user_id   : 2473, //The ID of your user
        details   : { //Optional
            email: 'castle@castle.io'
        }.
    }).then(obj => {
        //Handle success
        //Note that "obj" is almost always just {}
    }).catch(e => {
        //Handle error
        next(e)
    });
}

Identifying Users

(request, response, next) => {
    request.castleIdentify({
        user_id   : 2473, The ID of your user
        user_data   : { //Optional
            email: 'castle@castle.io'
        }
    }).then(obj => {
        //Handle success
        //Note that "obj" is almost always just {}
    }).catch(e => {
        //Handle error
        next(e)
    });
}

Options

The Castle object accepts these options upon initialization

CodeDescriptionDefault
apiKeyYour api key. This is currently unusednull
apiSecretYour api secret. This is used for authenticating youundefined
apiUrlThe endpoint you want to send your api requests tohttps://api.castle.io
disableClientUserAgentWether or not you want to send SDK info and OS information to castle for analyticsfalse

Events

These are the events Available through Castle.Events

CodeDescriptionDefault
LOGIN_SUCCEEDED$login.succeededRecord when a user attempts to log in
LOGIN_FAILED$login.failedRecord when a user logs out
LOGOUT_SUCCEEDED$logout.succeededRecord when a user logs out
REGISTRATION_SUCCEEDED$registration.succeededCapture account creation, both when a user signs up as well as when created manually by an administrator
REGISTRATION_FAILED$registration.failedRecord when an account failed to be created
EMAIL_CHANGE_REQUESTED$email_change.requestedAn attempt was made to change a user’s email
EMAIL_CHANGE_SUCCEEDED$email_change.succeededThe user completed all of the steps in the email address change process and the email was successfully changed
EMAIL_CHANGE_FAILED$email_change.failedUse to record when a user failed to change their email address
PASSWORD_RESET_REQUESTED$password_reset.requestedAn attempt was made to reset a user’s password
PASSWORD_RESET_SUCCEEDED$password_reset.succeededThe user completed all of the steps in the password reset process and the password was successfully reset. Password resets do not required knowledge of the current password
PASSWORD_RESET_FAILED$password_reset.failedUse to record when a user failed to reset their password
PASSWORD_CHANGE_SUCCEEDED$password_change.succeededUse to record when a user changed their password. This event is only logged when users change their own password
PASSWORD_CHANGE_FAILED$password_change.failedUse to record when a user failed to change their password
CHALLENGE_REQUESTED$challenge.requestedRecord when a user is prompted with additional verification, such as two-factor authentication or a captcha
CHALLENGE_SUCCEEDED$challenge.succeededRecord when additional verification was successful
CHALLENGE_FAILED$challenge.failedRecord when additional verification failed

Errors

Whenever something unexpected happens, a error is created and returned. Here's a list of errors that we're shamefully created

CodeDescription
MISSING_EVENT_NAMEYou've missed the event parameter for the trackEvent function
INVALID_HTTP_STATUS_CODEThe HTTP Code returned by the Castle API was unexpected
0.3.5

8 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.2.1

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago