• Stars
    star
    637
  • Rank 67,906 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 11 years ago
  • Updated about 5 years ago

Reviews

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

Repository Details

Authentication framework for Go applications.

Gomniauth

Authentication framework for Go applications.

  • EDITOR NOTE: It is recommended that you transition to the Goth package.

Features

Documentation

Get started

Install Gomniauth by doing:

$ go get github.com/stretchr/gomniauth

Check out the example web app code to see how to use Gomniauth using Goweb.

Contributing

  • If you add a provider that others could also make use of, please send us a Pull Request and we'll add it to the repo.

Implementing Gomniauth

Set up Gomniauth

First and only once for your application, you need to setup the security key and providers. The security key is used when hashing any data that gets transmitted to ensure its integrity.

You are free to use the signature package's RandomKey function to generate a unique code every time your application starts.

gomniauth.SetSecurityKey(signature.RandomKey(64))

A provider represents an authentication service that will be available to your users. Usually, you'll have to add some configuration of your own, such as your application key and secret (provided by the auth service), and the callback into your app where users will be sent following successful (or not) authentication.

gomniauth.WithProviders(
  github.New("key", "secret", "callback"),
  google.New("key", "secret", "callback"),
)

What kind of callback?

The callback should be an absolute URL to your application and should include the provider name in some way.

For example, in the example web app we used the following format for callbacks:

http://mydomain.com/auth/{provider}/callback

Are they logged in?

When a user tries to access a protected resource, or if you want to make third party authenticated API calls, you need a mechanism to decide whether a user is logged in or not. For web apps, cookies usually work, if you're building an API, then you should consider some kind of auth token.

Decide how to log in

If they are not logged in, you need to provide them with a list of providers from which they can choose.

You can access a list of the providers you are supporting by calling the gomniauth.Providers() function.

Redirecting them to the login page

Once a provider has been chosen, you must redirect them to be authenticated. You can do this by using the gomniauth.Provider function, that will return a Provider by name.

So if the user chooses to login using Github, you would do:

provider, err := gomniauth.Provider("github")

Once you have your provider, you can get the URL to redirect users to by calling:

authUrl, err := provider.GetBeginAuthURL(state, options)

You should then redirect the user to the authUrl.

State and options

The state parameter is a State object that contains information that will be hashed and passed (via the third party) through to your callback (see below). Usually, this object contains the URL to redirect to once authentication has completed, but you can store whatever you like in here.

The options parameter is an objx.Map containing additional query-string parameters that will be sent to the authentication service. For example, in OAuth2 implementations, you can specify a scope parameter to get additional access to other services you might need.

Handling the callback

Once the third party authentication service has finished processing the request, they will send the user back to your callback URL.

Remember, you specified the callback URL when you setup your providers.

For example, the user might hit:

http://yourdomain.com/auth/github/callback?code=abc123

You don't need to worry about the detail of the parameters passed back, because Gomniauth takes care of those for you. You just need to pass them into the CompleteAuth method of your provider:

provider, err := gomniauth.Provider("github")
creds, err := provider.CompleteAuth(queryParams)

NOTE: It's unlikely you'll hard-code the provider name, we have done it here to make it obvious what's going on.

The provider will then do the work in the background to complete the authentication and return the creds for that user. The creds are then used to make authenticated requests to the third party (in this case Github) on behalf of the user.

Getting the user information

If you then want some information about the user who just authenticated, you can call the GetUser method on the provider (passing in the creds from the CompleteAuth method.)

The User you get back will give you access to the common user data you will need (like name, email, avatar URL etc) and also an objx.Map of Data() that contains everything else.

Caching in

Once you had the credentials for a user for a given provider, you should cache them in your own datastore. This will mean that if the cookie hasn't expired, or if the client has stored the auth token, they can continue to use the service without having to log in again.

More Repositories

1

testify

A toolkit with common assertions and mocks that plays nicely with the standard library
Go
22,028
star
2

arg.js

Lightweight URL argument and parameter parser
JavaScript
1,060
star
3

objx

Go package for dealing with maps, slices, JSON and other data.
Go
670
star
4

goweb

A lightweight RESTful web framework for Go
Go
632
star
5

over.js

Elegant function overloading in JavaScript.
JavaScript
147
star
6

gorc

Recursive go testing, done better.
Go
141
star
7

powerwalk

Package for concurrently walking files
Go
104
star
8

stew

Stew is a very high performance package that extends common Go objects providing better alternatives or wrappers.
Go
67
star
9

hoard

A fast, smart caching package for Go
Go
59
star
10

github-stars

Returns the aggregate number of stars for a user/org or single repo
JavaScript
52
star
11

signature

URL signing package for Go
Go
38
star
12

codecs

Provides interfaces, functions and codecs that can be used to encode/decode data to/from various formats.
Go
31
star
13

filetypes.js

A complete list of file types, extensions and mime types in JavaScript.
JavaScript
18
star
14

pat

Go package for patterns and best practices
Go
15
star
15

readcaster

Go package for elegantly broadcasting one io.Reader source to many io.Readers
Go
14
star
16

commander

A Go package that makes it easy to create a command line interface.
Go
12
star
17

todo

A Go package that helps you remember the DO in TODO
Go
12
star
18

ottox

Plugins for the Otto Go JavaScript parser and interpreter
Go
12
star
19

thru

The worlds simplest web server
Go
11
star
20

slog

Logging package for Go
Go
10
star
21

jsonblend

A command line tool providing extensive capabilities for combining json objects.
Go
9
star
22

piglatin

English -> Pig Latin translator package for Go - TDD tutorial code
Go
9
star
23

respond

Go package for building data APIs
Go
7
star
24

sdk-go

Go SDK for Stretchr platform
Go
7
star
25

pools

pools is a go package for managing a suite of differently sized slices of objects backed by sync.Pool
5
star
26

tracer

A quick and dirty tracing package for Go
Go
5
star
27

bits

A simple bit manipulation package for Go.
Go
4
star
28

tdd-present

TDD Presentation code
Go
4
star
29

sdk-cocoa

Cocoa and iOS SDK for the Stretchr Platform
Objective-C
3
star
30

fanjs

JavaScript code for fanning elements in a circular pattern
JavaScript
3
star
31

sdk-ruby

Ruby SDK for the Stretchr Platform
Ruby
2
star
32

excerpt-search

Returns a highlighted excerpt from a block of text based on search terms
JavaScript
2
star
33

stretchr.github.io

The Stretchr.com homepage
JavaScript
2
star
34

jbaas

jsonblend as a service.
Go
2
star
35

sdk-js

HTML5 and JavaScript SDK for Stretchr
JavaScript
2
star
36

jsonblend-website

jsonblend website
JavaScript
2
star
37

config

Package that provides easy configuration management for Go programs
Go
2
star
38

sandiego

Repo for San Diego crime data workshop projects
2
star
39

cmds

Elegant command line and external tool management package for Go
Go
2
star
40

issues

Bug tracking for Stretchr platform
1
star
41

TagSpot

iOS application and HTML page showing example integrations into Stretchr
Objective-C
1
star
42

stretchr-backbone

Backbone.js adapter for Stretchr
JavaScript
1
star