• Stars
    star
    105
  • Rank 328,196 (Top 7 %)
  • Language
    JavaScript
  • Created about 12 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HTTP Basic Authentication for Backbone.

Backbone Basic Auth plugin

This plugin enables access to remote resources which are protected by HTTP Basic Authentication through your Backbone Models and Collections.

Modes

You can set Basic Authentication credentials in two ways:

  • Via a separate model/collection property: credentials.
  • Directly on the model/collection url property.

If you are unsure which mode to pick, use the credentials property on the model.

model.credentials

Usage:

var Model = Backbone.Model.extend({
  url: 'http://path/to/basic/auth/protected/resource'
});

var model = new Model();
model.credentials = {
	username: 'user',
	password: 'pass'
};

// or ...
model.credentials = function() {
	return {
		username: 'user',
		password: 'pass'
	};
};

model.fetch();

This mode is good for authentication that may change when the app is used, e.g. if different users are able to authenticate with the app. The credentials can be hard coded or set dynamically.

This mode is the most flexible. If you are unsure which mode to use, try this one first.

model.url

Usage:

var Model = Backbone.Model.extend({
  url: 'http://username:password@path/to/basic/auth/protected/resource'
});

var model = new Model();
model.fetch();

This mode is good for models where the username/password is unlikely to change, e.g. a fixed API key for your app. The plugin takes care of parsing the URL to create the necessary Basic Authentication header, and jQuery removes the credentials from the URL so your username and password isn't sent to the server directly on the URL.

Thanks goes to Luis Abreu for his work implementing this.

How does it work?

A resource protected with HTTP Basic Authentication requires the following HTTP header to be set on every request:

Authorization: Basic <accesstoken>

The access token is formed by taking the username and password, concatenating together with a : separator and encoding into Base64.

This plugin handles the Base64 encoding and automatically sets the Authorization header on every request which uses Backbone.sync.

Creating header for use elsewhere

On occasion, it can be useful to bypass Backbone.sync and use raw $.ajax (for example, when hitting server API resources which do not conform to RESTful principles). More information on this subject can be found in this excellent blog post by Derick Bailey.

If you need/want to do this, there is a convenience function which will help you build the BasicAuth header: Backbone.BasicAuth.getHeader(), which can be used to create the header to be set directly on the AJAX request.

Example:

// Pass the credentials to the plugin to build the header
$.ajax({
  method: 'GET',
  dataType: "json",
  url: 'http://path/to/basic/auth/protected/resource',
  headers: Backbone.BasicAuth.getHeader({
  	username: 'user',
  	password: 'pass'
	})
});

Dependencies

Server-side

The idea of this plugin is to adhere to the standard HTTP Basic Authentication scheme. There is bound to be a 'basic' way to read the username / password combination in your chosen server-side language.

See here for a PHP example.

Change Log

v0.4.2 (30th June 2016)

  • Added support for CommonJS.

v0.4.1 (16th October 2015)

  • Integrated package.json for use with npm.

v0.4.0 (23rd July 2013)

  • Re-introduced concept of setting Basic Auth credentials from a function, in addition to URL-based method.

v0.3.0 (22nd July 2013)

  • Moved to use the standard method of HTTP Basic Authentication, by adding username:password to the URL string. The old method of setting the Basic Auth token (Backbone.BasicAuth.set / Backbone.BasicAuth.remove) has now been removed from the codebase. Thanks to Luis Abreu for the PR.
  • Added AMD support (thanks again Luis Abreu).

v0.2.0 (1st May 2013)

  • Added Bower support.

License

Copyright 2013, Tom Spencer (@fiznool), Luis Abreu (@lmjabreu).

backbone.basicauth.js may be freely distributed under the MIT license.

More Repositories

1

express-mongo-sanitize

Sanitize your express payload to prevent MongoDB operator injection.
JavaScript
216
star
2

passport-oauth2-refresh

A passport.js add-on to provide automatic OAuth 2.0 token refreshing.
JavaScript
190
star
3

ng-elastic

Angular/Ionic 2 directive to auto expand textareas according to their contents.
TypeScript
49
star
4

body-parser-xml

XML parser middleware for express.js.
JavaScript
37
star
5

tappivate

A small library to help make your mobile web buttons and lists feel a little more app-y.
JavaScript
33
star
6

sugar-date

A customised build of sugar.js containing only the date functions.
JavaScript
12
star
7

poirot

A simple repo to showcase private repository deployment.
Shell
11
star
8

ng-imgcache

TypeScript
6
star
9

mean-docker-example

JavaScript
5
star
10

GumtreeScraper

Java
2
star
11

Alinea-iPad-Demo

A demo iPad app to showcase retail-specific features.
JavaScript
2
star
12

backbone.sentry

Protect your Backbone Routes like a Sentry.
JavaScript
2
star
13

SweepBook

JavaScript
2
star
14

phonegap-animated-ios

Objective-C
2
star
15

no-rest-for-the-whippet

A tutorial for creating a test-driven REST API using Mongoose, Express, Node and Supertest. And dogs.
JavaScript
2
star
16

passportjs-example

JavaScript
1
star
17

ionic-prod-flag-tester

CSS
1
star
18

Recognizr

A small library to detect capabilities of mobile browsers.
JavaScript
1
star
19

Backbone-ZombieNation

A small library which helps to manage bindings and subviews in a Backbone Application.
JavaScript
1
star
20

SafariApps-Prototype

Prototype for SafariApps.
JavaScript
1
star
21

tomspencer.dev

Astro
1
star
22

norrisbot

JavaScript
1
star
23

ng-component-router-example

JavaScript
1
star
24

catinator

JavaScript
1
star