go-auth: github.com/dukex/go-auth Index | Files

package auth

import "github.com/dukex/go-auth"

This auth provides sign in and sign up by oauth2 and email/password. Inspired in omniauth and devise gem

Index

Package Files

auth.go hash.go http.go provider.go user.go

Variables

var EmailPasswordProvider = Provider{
    Name: "emailpassword",
}

Email/Password default provider

func GenerateHash Uses

func GenerateHash(data string) (string, error)

GenerateHash wrapper to bcrypt.GenerateFromPassword

func NewUserToken Uses

func NewUserToken() string

type Auth Uses

type Auth struct {
    Providers map[string]*builderConfig
    Helper    UserHelper
}

Auth is the Authentication config, this store providers and UserHelper interface

func NewAuth Uses

func NewAuth() *Auth

func (*Auth) Authorize Uses

func (a *Auth) Authorize(providerName string) http.HandlerFunc

Authorize is a middleware to authorize user in a defined provider. Send provider name as params and the return is a http handle

GET   /auth/google     auth.Authorize("google")
GET   /auth/facebook   auth.Authorize("facebook")
POST  /sign_in         auth.Authorize("email")

func (*Auth) CurrentUser Uses

func (a *Auth) CurrentUser(r *http.Request) (id string, ok bool)

CurrentUser func expect you send the request(“`http.Request“`) and return the user id as string and bool true if is OK

func (*Auth) Login Uses

func (a *Auth) Login(r *http.Request, userId string) *sessions.Session

func (*Auth) Logout Uses

func (a *Auth) Logout(r *http.Request) *sessions.Session

func (*Auth) NewProvider Uses

func (a *Auth) NewProvider(p Provider)

func (*Auth) NewProviders Uses

func (b *Auth) NewProviders(providers []Provider)

func (*Auth) OAuthCallback Uses

func (a *Auth) OAuthCallback(providerName string, w http.ResponseWriter, r *http.Request) (string, error)

GET /auth/facebook/callback http.HandlerFunc -> auth.OAuthCallback("facebook", w, r) “`

func (*Auth) SignIn Uses

func (a *Auth) SignIn(w http.ResponseWriter, r *http.Request) (string, bool)

HTTP Handler to sign in users is expected email and password in request body as JSON

{"email": "myemail@domain.com", "password": "abc123"}

type Provider Uses

type Provider struct {
    Name        string
    Key         string
    Secret      string
    RedirectURL string
    TokenURL    string
    AuthURL     string
    UserInfoURL string
    Scopes      []string
}

Provider is a oauth2 provider, like facebook or google

Name is the provider name, the package use it as a index.
Key is the oauth2 key, normally called ACCESS_KEY
Secret is the oauth2 secret key, normally called ACCESS_SECRET
RedirectURL is a URL configured in provider
TokenURL is a URL to get the token on provider
AuthURL is a URL to auth user on provider
UserInfoURL is a URL to get User Information on provider
Scope is whats the scope your app wants

type User Uses

type User struct {
    Id      string
    Email   string
    Link    string
    Name    string
    Gender  string
    Locale  string
    Picture string
    Token   string
}

type UserHelper Uses

type UserHelper interface {
    PasswordByEmail(email string) (string, bool)
    FindUserDataByEmail(email string) (string, string, bool)
    FindUserByToken(token string) (string, bool)
    FindUserFromOAuth(provider string, user *User, rawResponse *http.Response) (string, error)
}

UserHelper interface has some important method to auth works

PasswordByEmail func(email string) (string, bool)

Called when user sign in by email/password to get user password and check with inputed password, the method will send user email as string and expect the user password as string

FindUserDataByEmail func(email string) (string, bool)

Should returns a user data in string format(json/xml) Will be use to SignIn handler after user SignIn

Package auth imports 11 packages (graph) and is imported by 3 packages. Updated 2016-10-02. Refresh now. Tools for package owners.