Documentation ¶
Overview ¶
Package auth defines Authentication and Authorization interfaces which are used in to authenticate and authorized endpoints.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AllowAll ¶
type AllowAll struct{}
AllowAll allows all requests
func (*AllowAll) Authenticate ¶
Authenticate always allows a request
type Authenticator ¶
type Authenticator interface { Authenticate(interface{}) error Authorizor() Authorizor }
Authenticator authorizes access to an endpoint. If error is nil, then the request is authorized. The error should contain useful information which identifies why the authorization failed
Example ¶
package main import ( "errors" "fmt" "net/http" "bitbucket.org/cvvs/auth" ) type basicAuth struct { Users map[string]string } func (ba *basicAuth) Authenticate(input interface{}) error { req, ok := input.(*http.Request) if !ok { return errors.New("basic auth: parameter is not an http request") } username, password, ok := req.BasicAuth() if !ok { return errors.New("basic auth: not supported") } stored, ok := ba.Users[username] if !ok { return errors.New("basic auth: no such user " + username) } else if password == stored { return nil } return errors.New("basic auth: password mismatch") } func (ba *basicAuth) Authorizor() auth.Authorizor { return new(auth.AllowAll) } func main() { var auth auth.Authenticator = &basicAuth{Users: map[string]string{"luggage": "12345"}} req, _ := http.NewRequest(http.MethodGet, "http://www.example.com", nil) req.SetBasicAuth("luggage", "12345") if err := auth.Authenticate(req); err != nil { fmt.Println("error authenticating user: ", err) } if err := auth.Authorizor().Authorize(req); err != nil { fmt.Println("error authorizing user") } }
Output:
type Authorizor ¶
type Authorizor interface {
Authorize(interface{}) error
}
Authorizor authorizes access to an endpoint. If error is nil, then the request is authorized. The error should contain useful information which identifies why the authorization failed
Click to show internal directories.
Click to hide internal directories.