Giter VIP home page Giter VIP logo

javascript-oauth2's Introduction

javascript-oauth2

An implementation of an OAuth2 client in pure JavaScript for web applications, licensed under the 3-clause BSD license.

Overview

Provides a window.oauth2 object, containing a OAuth2XMLHttpRequest class implementing the XMLHttpRequest interface for making OAuth2-protected requests.

Here's the feature list:

  • Transparently handles 401 Unauthorized responses from the remote web service.
  • Provides a hook for the application to prompt the user to visit the remote web service to authorize the application.
  • Transparently refreshes expired access tokens if a refresh token has previously been provided.
  • Wraps a XMLHttpRequest or XDomainRequest object, or something that acts like one.
  • Supports Bearer authentication.

Requirements

The remote web service and browser must both support Cross-Origin Resource Sharing (CORS) on the protected resource.

The OAuth2 token endpoint should ideally return the following headers with their responses:

Access-Control-Allow-Origin: https://your-domain
Access-Control-Expose-Headers: WWW-Authenticate

All is not lost if it doesn't; the library will make intelligent guesses in the dark.

Each protected resource must support preflighted requests. Here's an example request and response:

OPTIONS /protected-resource
Access-Control-Request-Headers: authorization
Access-Control-Request-Method: PUT

Allow: GET,POST,PUT,DELETE,HEAD
Access-Control-Allow-Headers: authorization
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,HEAD
Access-Control-Allow-Origin: https://your-domain
Access-Control-Expose-Headers: WWW-Authenticate

It may be simplest to mirror the Access-Control-Request-Headers request header to the Access-Control-Allow-Headers response header, and to duplicate the Allow response header (listing all available methods) to the Access-Control-Allow-Methods response header. Note that the response should be a 200 OK or 204 No Content, even if a non-OPTIONS request would return 401 Unauthorized.

The web service must respond to requests requiring authentication with 401 Unauthorized, not a redirect to a login form. In time, we should support pre-emptive authorization and checking for login page redirects.

You can use OAuth2XMLHttpRequest with jQuery like this:

$.ajax('https://example.com/', {
    xhr: oauth2.factory({
        authorizeEndpoint: 'https://example.com/oauth2/authorize',
        tokenEndpoint: 'https://example.com/oauth2/token',
        clientID: 'abcdefgh',
        clientSecret: 'ijklmnop',
        localStoragePrefix: 'oauth2.com.example'
    },
    ...
});

Browser support

This has been tested in:

  • Google Chrome 23
  • Firefox 17 (xhr.getResponseHeader() support on CORS requests is broken, but worked around)
  • Opera 12

It is believed that it should work in:

  • Internet Explorer 8+

It does not work in:

  • Android 2.3.3 Browser (intercepts the 401 response before we can do anything about it)

Security considerations

If your web application is served over HTTP, an attacker will be able to intercept the OAuth2 authorization code added to the redirection URI. See Section 4.12 of the OAuth 2.0 specification for further details. If your application is on the public web, an attacker will also have access to the client secret, and will be able to combine them to request an access token in order to imitate the authenticated user. It is strongly RECOMMENDED that your application is served over HTTPS.

If your application is served from the same domain as untrusted code (such as when using Apache's UserDir directive to host sites at e.g. http://users.example.org/~alice/), that other code will be able to access the OAuth2 access token from local storage, and will be able to make authenticated requests. It is strongly RECOMMENDED that all JavaScript on your application's domain is trusted.

Example

Here's a minimal example:

var xhr = new oauth.OAuth2XMLHttpRequest({
    authorizeEndpoint: "https://example.com/oauth2/authorize",
    tokenEndpoint: "https://example.com/oauth2/token",
    clientID: "client id",
    clientSecret: "client secret",
    localStoragePrefix: "oauth2.example.", // Used for storing credentials in localStorage
    requestAuthorization: function(callback) {
        /* This function will be called if the user is required to visit the *
         * remote web service to authorize the application. If the user      *
         * consents, call callback() to open a pop-up window.                */

        // Let's use the jQuery UI dialog (http://jqueryui.com/dialog/)
        $('#dialog-authorize').dialog({
            resizable: false,
            width: 500,
            modal: true,
            buttons: {
                "Proceed": function() {
                    $(this).dialog("close");
                    callback();
                },
                "Cancel": function() {
                    $(this).dialog("close");
                }
            }
        });
    }
});

xhr.onreadystatechange = function() {
   // get something
};
xhr.open('GET', 'https://example.com/protected-resource');
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('param=value&otherparam=othervalue');

javascript-oauth2's People

Contributors

ahaith avatar alexdutton avatar discordianfish avatar paulrouget avatar sompylasar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

javascript-oauth2's Issues

Security issue maybe?

I see you are having client secret and client id in code:

   clientID: 'abcdefgh',
   clientSecret: 'ijklmnop',

What if I change /etc/hosts and put redirect url (dmina) from ouath there?

Reduce dependency on CORS

Requests against the token endpoint do not require CORS.

It is possible to send the access token as a query parameter or form-encoded body parameter (see http://tools.ietf.org/html/rfc6750#section-2.2 ff.), which would not need CORS for GET, HEAD and POST requests (simple methods, as per the CORS spec).

We should try CORS requests first, and fall back to non-CORS requests by default, with applications able to override to specify one or the other.

If the remote resource doesn't support CORS, our inclusion of an Authorization request header will cause the cross-origin request status to be set to network error, causing us to fall back to non-CORS.

Non-simple request methods will still require CORS, but these aren't going to be the primary use-case for most users.

client secret, client id

Don't you think that it is kind of of security issue if i put client secret and client id into javascript? That means that I can fake domain by just changing /etc/hosts file and grant access to what ever I wont?

Make dependency on Underscore / Lodash explicit

When trying to use the library, I'm getting:

ReferenceError: _ is not defined

I don't see this anywhere in the docs.

Ideally, you may consider publishing this on NPM and/or Bower, which would allow dependency specification (Underscore or Lodash, and anything else needed would be installed along) and, in the case of the former, would allow the library to be usable without attaching itself to the global scope (with minimal code changes).

Thanks!

Wrapper around XMLHttpRequest

Remove the dependency on jQuery by implementing a wrapper around XMLHttpRequest (which could then be passed to jQuery.ajax().

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.