Documentation ¶
Overview ¶
Package oauth1 allows users to be created and authenticated via oauth1 services like facebook, google etc. Currently only the web server flow is supported.
The general flow looks like this:
- User goes to Start handler and has his session packed with goodies then redirects to the OAuth service.
- OAuth service returns to OAuthCallback which checks that everything is ok. It uses the token received to get an access token and secret from the oauth1 library
- Calls the OAuth1Provider.FindUserDetails which should return the user's details in a generic form.
- Passes the user details into the ServerStorer.NewFromOAuth1 in order to create a user object we can work with.
- Saves the user in the database, logs them in, redirects.
In order to do this there are a number of parts:
- The configuration of a provider (handled by OAuth1Providers).
- The flow of redirection of client, parameter passing etc (handled by this package)
- The HTTP call to the service once a token has been retrieved to get user details (handled by OAuth1Provider.FindUserDetails)
- The creation of a user from the user details returned from the FindUserDetails (authboss.ServerStorer)
Of these parts, the responsibility of the authboss library consumer is on 1, 3, and 4. Configuration of providers that should be used is totally up to the consumer. The FindUserDetails function is typically up to the user, but we have some basic ones included in this package too. The creation of users from the FindUserDetail's map[string]string return is handled as part of the implementation of the ServerStorer.
Index ¶
- Constants
- Variables
- func MakeOAuth1PID(provider, uid string) string
- func ParseOAuth1PID(pid string) (provider, uid string, err error)
- func TwitterFindUserDetails(ctx context.Context, config oauth1.Config, token oauth1.Token) (map[string]string, error)
- type Config
- type OAuth1
- type Provider
- type RMTrue
- type ServerStorer
- type Token
- type User
Constants ¶
const ( // SessionOAuth1Secret is the request secret created during the login flow. SessionOAuth1Secret = "oauth1_secret" // SessionOAuth1Params is the additional settings for oauth // like redirection/remember. SessionOAuth1Params = "oauth1_params" // EventOAuth1Fail For Authboss events EventOAuth1 authboss.Event = 233234 // random to avoid collision EventOAuth1Fail authboss.Event = 249847 // random to avoid collision FormValueOAuth1Redir = "redir" )
FormValue constants
Variables ¶
var ( // Providers are the registered OAuth1 providers Providers = make(map[string]Provider) // LoginOK is the path to redirec to on a successful login LoginOK = "/" // LoginNotOK is the path to redirec to on a failed login LoginNotOK = "/" )
Functions ¶
func MakeOAuth1PID ¶
MakeOAuth1PID is used to create a pid for users that don't have an e-mail address or username in the normal system. This allows all the modules to continue to working as intended without having a true primary id. As well as not having to divide the regular and oauth stuff all down the middle.
func ParseOAuth1PID ¶
ParseOAuth1PID returns the uid and provider for a given OAuth1 pid
Types ¶
type OAuth1 ¶
type OAuth1 struct {
*authboss.Authboss
}
OAuth1 module
type Provider ¶
type Provider struct { Config *oauth1.Config AdditionalParams url.Values FindUserDetails func(context.Context, oauth1.Config, oauth1.Token) (map[string]string, error) }
Provider represents all we need to register an OAuth1 Provider
func TwitterProvider ¶
TwitterProvider is a helper function to created a twitter oauth1 provider
type RMTrue ¶
type RMTrue struct{}
RMTrue is a dummy struct implementing authboss.RememberValuer in order to tell the remember me module to remember them.
func (RMTrue) GetShouldRemember ¶
GetShouldRemember always returns true
type ServerStorer ¶
type ServerStorer interface { authboss.ServerStorer // NewFromOAuth1 should return an OAuth1User from a set // of details returned from OAuth1Provider.FindUserDetails // A more in-depth explanation is that once we've got an access token // for the service in question (say a service that rhymes with book) // the FindUserDetails function does an http request to a known endpoint // that provides details about the user, those details are captured in a // generic way as map[string]string and passed into this function to be // turned into a real user. // // It's possible that the user exists in the database already, and so // an attempt should be made to look that user up using the details. // Any details that have changed should be updated. Do not save the user // since that will be done later by ServerStorer.SaveOAuth1() NewFromOAuth1(ctx context.Context, provider string, details map[string]string) (User, error) // SaveOAuth1 has different semantics from the typical ServerStorer.Save, // in this case we want to insert a user if they do not exist. // The difference must be made clear because in the non-oauth1 case, // we know exactly when we want to Create vs Update. However since we're // simply trying to persist a user that may have been in our database, // but if not should already be (since you can think of the operation as // a caching of what's on the oauth1 provider's servers). SaveOAuth1(ctx context.Context, user User) error }
ServerStorer has the ability to create users from data from the provider.
func EnsureCanOAuth1 ¶
func EnsureCanOAuth1(storer authboss.ServerStorer) ServerStorer
EnsureCanOAuth1 makes sure the server storer supports oauth1 creation and lookup
type User ¶
type User interface { authboss.User // IsOAuth1User checks to see if a user was registered in the site as an // oauth1 user. IsOAuth1User() bool GetOAuth1UID() (uid string) GetOAuth1Provider() (provider string) GetOAuth1AccessToken() (token string) GetOAuth1AccessSecret() (secret string) PutOAuth1AccessToken(token string) PutOAuth1AccessSecret(secret string) }
User allows reading and writing values relating to OAuth1