socialauth

package module
v0.0.0-...-10373e3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2016 License: MIT Imports: 12 Imported by: 0

README

#SocialAuth.go

The SocialAuth package enables you to verify the OAuth2 authentication tokens issued by social providers.

At the moment only two providers are available (Facebook and Google).

Installation

go get github.com/dimitark/socialauth
import "github.com/dimitark/socialauth"

Usage

Configuration

You need to configure the library, before you can use it.

// Configure the SocialAuth
socialAuth := socialauth.NewSocialAuth(
	socialauth.NewFacebookAuthProvider("YOUR_APP_ACCESS_TOKEN"),
	socialauth.NewGoogleAuthProvider("YOUR_GOOGLE_APP_CLIENT_ID"),
)

The library can be used in two ways:

  • independently
  • as middleware

Independent usage

if provider := socialAuth.Get("facebook"); provider != nil {
	userID, err := provider.VerifyToken("USER_TOKEN")
	if err != nil {
		// Authentication failed!
	} else {
		// Success
	}
}

Middleware usage

You can use it as a middleware for both negroni OR gorilla

An example using negroni:

n := negroni.New()
n.Use(socialauth.NewMiddleware(socialAuth, context.Set))

License

The MIT License (MIT)

Copyright (c) 2016, Dimitar Kotevski

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Documentation

Index

Constants

View Source
const ContextUserID = "SocialAuthUserID"

ContextUserID is the key under which the UserID of the current user is stored in the given context, through the ContextSetFunc Only for Authenticated requests

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthProvider

type AuthProvider interface {
	// Identifier returns the string identifier of the provider
	Identifier() string

	// Verifies the given token against the server's provider (Facebook, Google...)
	// And returns the user ID or an error
	VerifyToken(userToken string) (string, error)
}

AuthProvider is an interface that provides a method for token verification

type ContextSetFunc

type ContextSetFunc func(*http.Request, interface{}, interface{})

ContextSetFunc is the function that sets a value for a given key and request in a context. Usually this is the Gorilla Context -> context.Set -> github.com/gorilla/context

type FacebookAuthProvider

type FacebookAuthProvider struct {
	// contains filtered or unexported fields
}

FBAuthProvider - The Facebook Auth provider implementation

func NewFacebookAuthProvider

func NewFacebookAuthProvider(appAccessToken string) *FacebookAuthProvider

Creates a new Facebook Authentication Provider instance The required appAccessToken can be obtained by Facebook's API -> https://developers.facebook.com/docs/facebook-login/access-tokens/#apptokens

func (*FacebookAuthProvider) Identifier

func (p *FacebookAuthProvider) Identifier() string

Returns the identifier of this provider

func (*FacebookAuthProvider) VerifyToken

func (p *FacebookAuthProvider) VerifyToken(userToken string) (string, error)

VerifyToken verifies the given token against the Facebook's server and returns the user ID or an error

type GoogleAuthProvider

type GoogleAuthProvider struct {
	// contains filtered or unexported fields
}

The GoogleAuthProvider validates the token against the google api

func NewGoogleAuthProvider

func NewGoogleAuthProvider(appClientId string) *GoogleAuthProvider

Creates a new Google Authentication Provider The required appClientId can be found in your Google API's Dashboard https://developers.google.com/identity/protocols/OAuth2

func (*GoogleAuthProvider) Identifier

func (p *GoogleAuthProvider) Identifier() string

Returns the identifier of this provider

func (*GoogleAuthProvider) VerifyToken

func (p *GoogleAuthProvider) VerifyToken(userToken string) (string, error)

VerifyToken verifies the given token against the server's provider (Facebook, Google...) And returns the user ID or an error

type GoogleTokenValidator

type GoogleTokenValidator struct {
	// contains filtered or unexported fields
}

GoogleTokenValidator validates the given token using the Google's public keys

func NewGoogleTokenValidator

func NewGoogleTokenValidator(appClientID string) *GoogleTokenValidator

NewGoogleTokenValidator creates a new instance of the GoogleTokenValidator struct Configures it and returns it

func (*GoogleTokenValidator) Validate

func (v *GoogleTokenValidator) Validate(token string) (string, error)

Validate validates the given token. If the token is invalid - it returns an error

type Middleware

type Middleware struct {
	// contains filtered or unexported fields
}

Middleware is a middleware for the http package It verifies if the request has a valid token The token is verified by one of the Social AuthProviders (Facebook, Google,...)

func NewMiddleware

func NewMiddleware(socialAuth *SocialAuth, contextSetFunc ContextSetFunc) *Middleware

NewMiddleware creates a new Middleware with the given configuration

func (*Middleware) ServeHTTP

func (am *Middleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

type SocialAuth

type SocialAuth struct {
	// contains filtered or unexported fields
}

SocialAuth holds all the configured providers Use an instance of this struct to work with this library

func NewSocialAuth

func NewSocialAuth(providers ...AuthProvider) *SocialAuth

NewSocialAuth creates and returns a new instance of the SocialAuth with the given providers

func (*SocialAuth) Get

func (sa *SocialAuth) Get(provider string) AuthProvider

Get returns the asked provider. Nil if such provider is not configured.

type TokenInfo

type TokenInfo struct {
	Sub           string `json:"sub"`
	Email         string `json:"email"`
	AtHash        string `json:"at_hash"`
	Aud           string `json:"aud"`
	EmailVerified bool   `json:"email_verified"`
	Name          string `json:"name"`
	GivenName     string `json:"given_name"`
	FamilyName    string `json:"family_name"`
	Picture       string `json:"picture"`
	Local         string `json:"locale"`
	Iss           string `json:"iss"`
	Azp           string `json:"azp"`
	Iat           int64  `json:"iat"`
	Exp           int64  `json:"exp"`
}

TokenInfo

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL