login2

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2015 License: MIT Imports: 12 Imported by: 3

README

Login2

Easy way to sign in and sign up users using oauth and email/password

$ go get gopkg.in/dukex/login2.v1
import "gopkg.in/dukex/login2.v1"
var loginBuilder *login2.Builder
loginBuilder = login2.NewBuilder()

Config

To config your oauth provider use NewProvider func

provider := &login2.Provider{
  RedirectURL: os.Getenv("GOOGLE_CALLBACK_URL"),
  AuthURL:     "https://accounts.google.com/o/oauth2/auth",
  TokenURL:    "https://accounts.google.com/o/oauth2/token",
  Name:        "google",
  Key:         os.Getenv("GOOGLE_CLIENT_ID"),
  Secret:      os.Getenv("GOOGLE_CLIENT_SECRET"),
  Scope:       "https://www.googleapis.com/auth/userinfo.email",
  UserInfoURL: "https://www.googleapis.com/oauth2/v1/userinfo?alt=json",
}

loginBuilder.NewProvider(provider)

The func NewProviders accept a Provider array

providers := make([]*login2.Provider, 0)

providers = append(providers, &auth.Provider{
  RedirectURL: os.Getenv("GOOGLE_CALLBACK_URL"),
  AuthURL:     "https://accounts.google.com/o/oauth2/auth",
  TokenURL:    "https://accounts.google.com/o/oauth2/token",
  Name:        "google",
  Key:         os.Getenv("GOOGLE_CLIENT_ID"),
  Secret:      os.Getenv("GOOGLE_CLIENT_SECRET"),
  Scope:       "https://www.googleapis.com/auth/userinfo.email",
  UserInfoURL: "https://www.googleapis.com/oauth2/v1/userinfo?alt=json",
})

loginBuilder.NewProviders(providers)

Login2 works with callback to be a agnostic way to sign in and sign up users, login2.Builder accept 4 callbacks

loginBuilder.UserSetupFn = func(provider string, user *auth.User, rawResponde *http.Response) (int64, error)  {
}

loginBuilder.UserCreateFn = func(email string, password string, request *http.Request) (int64, error) {
}

loginBuilder.UserIdByEmail = func(email string) (int64, error) {
}

loginBuilder.UserPasswordByEmail = func(email string) (string, error) {
}

loginBuilder.UserResetPasswordFn = func(token string, email string) {
}

To http handlers works you need config your URLs, login2 has URL type:

type URLS struct {
  Redirect                string
  SignIn                  string
  SignUp                  string
  ResetPasswordSuccess    string
}

And Builder has URLS field

loginBuilder.URLS = login2.URLS{
  Redirect: "/dashbaord",
  SignIn:    "/login",
  SignUp:  "/register",
  ResetPasswordSuccess: "/reset_password_success"
}

After your sign or sign up login2 will send user to Redirect url.

When login2 need sign in user, e.g User trying access protected path, login2 will send user to SignIn url.

When login2 need send up user, login2 will send user to SignUp url.

TODO: ResetPasswordSuccess

See Doc

Documentation

Overview

Package login2 provides sign in and sign up by oauth2 and email and password. Inspired in omniauth and devise gem

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewUserToken

func NewUserToken() string

Types

type Builder

type Builder struct {
	Providers           map[string]*builderConfig
	UserSetupFn         func(provider string, user *User, rawResponde *http.Response) (int64, error)
	UserCreateFn        func(email string, password string, token string, request *http.Request) (int64, error)
	UserResetPasswordFn func(token string, email string)
	UserIdByEmail       func(email string) (int64, error) // TODO: use bool as second params
	UserIdByToken       func(token string) (int64, bool)
	UserPasswordByEmail func(email string) (string, bool)
	LoginFn             func(userId string)
	URLS                URLS
}

Builder is the app configuration, store providers and callbacks

Follow the callbacks documentation:

UserSetupFn         func(provider string, user *login2.User, rawResponse *http.Response) (int64, error)

Called when user return from oauth provider, this method will send a provider origin as string, some user information as ```login2.User``` and the raw response from origin(login2 will make a request to ``` UserInfoURL``` configured on provider config). To sign in user the method expect the user id as int64

UserCreateFn        func(email string, password string, token string, request *http.Request) (int64, error)

Called when user sign up by email/password, the method will send email and password as string, password // is encrypted hash, and expect the user id as int64

UserIdByEmail       func(email string) (int64, error)

Called when user sign in by email/password to get the user id by email after check the password with ```UserPasswordByEmail```, the method will send the user email as string and expect the user id as int64

UserPasswordByEmail 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

UserResetPasswordFn func(token string, email string)

TODO

func NewBuilder

func NewBuilder() *Builder

func (*Builder) CurrentUser

func (b *Builder) 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 (*Builder) Login

func (b *Builder) Login(r *http.Request, userId string) *sessions.Session

func (*Builder) Logout

func (b *Builder) Logout(r *http.Request) *sessions.Session

func (*Builder) NewProvider

func (b *Builder) NewProvider(p *Provider)

func (*Builder) NewProviders

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

func (*Builder) OAuthAuthorize

func (b *Builder) OAuthAuthorize(provider string) func(http.ResponseWriter, *http.Request)

OAuthAuthorize To authorize user on defined provider. Send provider name as params and method will return http handle

```
GET   /auth/google     loginBuilder.OAuthAuthorize("google")
GET   /auth/facebook   loginBuilder.OAuthAuthorize("facebook")
```

func (*Builder) OAuthCallback

func (b *Builder) OAuthCallback(provider string, r *http.Request) (int64, error)

OAuthCallback receive code params from provider and get user information

func (*Builder) OAuthLogin

func (b *Builder) OAuthLogin(provider string) func(http.ResponseWriter, *http.Request)

OAuthLogin The oauth endpoint callback, configured on provider, Send provider name as params and method will return http handle

```
GET   /auth/callback/google     loginBuilder.OAuthLogin("google")
GET   /auth/callback/facebook   loginBuilder.OAuthLogin("facebook")
```

func (*Builder) Protected

func (b *Builder) Protected(fn func(string, http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request)

Protected to be used on protected path, send the original http handle as params and if user is logged Protected will pass user to original handler else Protected will save URL and send user to Sign In. Protected send as first params the user id.

```
GET   /dashboard   Protected(DashboardHandle)
```

func (*Builder) ResetPassword

func (b *Builder) ResetPassword() func(http.ResponseWriter, *http.Request)

func (*Builder) SetReturnTo

func (b *Builder) SetReturnTo(w http.ResponseWriter, r *http.Request, url string)

func (*Builder) SignIn

func (b *Builder) SignIn() func(http.ResponseWriter, *http.Request)

SignIn Handler to sign in user, send a http POST with email and password params on body

```
POST   /users/sign_in   SignIn
```

func (*Builder) SignOut

func (b *Builder) SignOut() func(http.ResponseWriter, *http.Request)

SignOut Handler Method to sign out user, send a http GET

```
GET   /users/sign_out   SignOut
```

func (*Builder) SignUp

func (b *Builder) SignUp() func(http.ResponseWriter, *http.Request)

SignUp Hanlder to sign up user, send a http POST with email and password params on body

```
POST   /users/sign_up   SignUp
```

type Provider

type Provider struct {
	Name        string
	Key         string
	Secret      string
	RedirectURL string
	TokenURL    string
	AuthURL     string
	UserInfoURL string
	Scope       string
}

Provider is a oauth2 provider, like facebook or google Name is provider name, it's like a key, will can be use it after, the package only use it as a index. Key is oauth2 key Secret is oauth2 secret key RedirectURL is a url will config on 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 URLS

type URLS struct {
	Redirect             string
	SignIn               string
	SignUp               string
	ResetPasswordSuccess string
}

type User

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

Jump to

Keyboard shortcuts

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