Golang OAuth2 server library
Go
Latest commit e09d96c Nov 30, 2016 @RangelReale committed on GitHub Merge pull request #137 from ShaleApps/add-osinredis
Add osinredis to README
Permalink
Failed to load latest commit information.
example Added jwt-go keys parsing in example/jwttoken/jwttoken.go Jul 29, 2016
.gitignore * Add examples Sep 10, 2013
.travis.yml Added Travis CI configuration Aug 15, 2014
CHANGELOG * BREAKING CHANGES, READ CHANGELOG! Jun 26, 2014
LICENSE * Initial import Sep 10, 2013
README.md Add osinredis Nov 30, 2016
access.go add detail to internal error messages Sep 13, 2016
access_test.go Add PKCE support Sep 10, 2016
authorize.go Add PKCE support Sep 10, 2016
authorize_test.go Add PKCE support Sep 10, 2016
client.go Allow encrypted client secrets Dec 8, 2015
client_test.go * BREAKING CHANGES, READ CHANGELOG! Jun 26, 2014
config.go Add PKCE support Sep 10, 2016
error.go DRY and simplify code, add comments, other small cleanup Nov 20, 2013
info.go Checking if bearer is nil in info request Feb 26, 2015
info_test.go Add support to retrieve access token information retrieving the token… Jan 7, 2015
response.go Fix preserving existing query parameters in redirect_uri Nov 28, 2016
response_json.go only set the Content-Type header when one hasn't already been set May 20, 2015
response_json_test.go * BREAKING CHANGES, READ CHANGELOG! Jun 26, 2014
response_test.go Fix preserving existing query parameters in redirect_uri Nov 28, 2016
server.go Remove duplicate code. Feb 19, 2015
storage.go type in docs "Storate" -> "Storage" Dec 14, 2014
storage_test.go Add PKCE support Sep 10, 2016
tokengen.go Replace custom removePadding with base64.RawURLEncoding Sep 13, 2016
urivalidate.go Match subpath correctly, prevent path traversals Jun 17, 2015
urivalidate_test.go Match subpath correctly, prevent path traversals Jun 17, 2015
util.go Refactor client secret checking Sep 10, 2016
util_test.go Add support to retrieve access token information retrieving the token… Jan 7, 2015

README.md

OSIN

GoDoc

Golang OAuth2 server library

OSIN is an OAuth2 server library for the Go language, as specified at http://tools.ietf.org/html/rfc6749 and http://tools.ietf.org/html/draft-ietf-oauth-v2-10.

It also includes support for PKCE, as specified at https://tools.ietf.org/html/rfc7636, which increases security for code-exchange flows for public OAuth clients.

Using it, you can build your own OAuth2 authentication service.

The library implements the majority of the specification, like authorization and token endpoints, and authorization code, implicit, resource owner and client credentials grant types.

Example Server

import "github.com/RangelReale/osin"

// TestStorage implements the "osin.Storage" interface
server := osin.NewServer(osin.NewServerConfig(), &TestStorage{})

// Authorization code endpoint
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
    resp := server.NewResponse()
    defer resp.Close()

    if ar := server.HandleAuthorizeRequest(resp, r); ar != nil {

        // HANDLE LOGIN PAGE HERE

        ar.Authorized = true
        server.FinishAuthorizeRequest(resp, r, ar)
    }
    osin.OutputJSON(resp, w, r)
})

// Access token endpoint
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
    resp := server.NewResponse()
    defer resp.Close()

    if ar := server.HandleAccessRequest(resp, r); ar != nil {
        ar.Authorized = true
        server.FinishAccessRequest(resp, r, ar)
    }
    osin.OutputJSON(resp, w, r)
})

http.ListenAndServe(":14000", nil)

Example Access

Open in your web browser:

http://localhost:14000/authorize?response_type=code&client_id=1234&redirect_uri=http%3A%2F%2Flocalhost%3A14000%2Fappauth%2Fcode

Storage backends

There is a mock available at example/teststorage.go which you can use as a guide for writing your own.

You might want to check out other implementations for common database management systems as well:

License

The code is licensed using "New BSD" license.

Author

Rangel Reale rangelreale@gmail.com

Changes

2014-06-25

  • BREAKING CHANGES:

    • Storage interface has 2 new methods, Clone and Close, to better support storages that need to clone / close in each connection (mgo)
    • Client was changed to be an interface instead of an struct. Because of that, the Storage interface also had to change, as interface is already a pointer.

    • HOW TO FIX YOUR CODE:

      • In your Storage, add a Clone function returning itself, and a do nothing Close.
      • In your Storage, replace all *osin.Client with osin.Client (remove the pointer reference)
      • If you used the osin.Client struct directly in your code, change it to osin.DefaultClient, which is a struct with the same fields that implements the interface.
      • Change all accesses using osin.Client to use the methods instead of the fields directly.
      • You MUST defer Response.Close in all your http handlers, otherwise some Storages may not clean correctly.

          resp := server.NewResponse()
          defer resp.Close()