// Authenticate process the request and returns a populated UserProfile. // If the Authenticate method can not authenticate the User based on the // request, an error or a redirect URL wll be return. func (p *Provider) Authenticate(w http.ResponseWriter, r *http.Request) ( up *profile.Profile, redirectURL string, err error) { c := context.NewContext(r) url := r.FormValue("provider") // Set provider info. up = profile.New(p.Name, url) // Check for current User. u := aeuser.Current(c) if u == nil { redirectURL := r.URL.Path + "/callback" loginUrl, err := aeuser.LoginURLFederated(c, redirectURL, url) return up, loginUrl, err } if u.FederatedIdentity != "" { up.ID = u.FederatedIdentity } else { up.ID = u.ID } per := new(person.Person) per.Email = u.Email per.Emails = []*person.PersonEmails{ &person.PersonEmails{true, "home", u.Email}, } per.URL = u.FederatedIdentity up.Person = per return up, "", nil }
// Link returns a LoginLink to allow the user to login with the provider. func (p Provider) Link(c appengine.Context, continueURL string) (Link, error) { url, err := user.LoginURLFederated(c, continueURL, p.Identifier) if err != nil { return Link{}, fmt.Errorf("failed to create login link for %q: %s", p.Name, err) } return Link{p.Name, url}, nil }
func loginfederated(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) if !printUserInfo(c, w, r) { // ログインURL(OpenID)にリダイレクト url, err := user.LoginURLFederated(c, r.URL.String(), "me.yahoo.co.jp") if err != nil { http.Error(w, err.String(), http.StatusInternalServerError) return } http.Redirect(w, r, url, http.StatusFound) } }
func openIdLogin(w http.ResponseWriter, r *http.Request) { var directProviders = []Provider{ {"Google", "https://www.google.com/accounts/o8/id"}, {"Yahoo", "yahoo.com"}, {"MySpace", "myspace.com"}, {"AOL", "aol.com"}, {"MyOpenID", "myopenid.com"}, } var usernameProviders = []Provider{ {"Blogger", "%s.blogspot.com"}, } w.Header().Set("Content-type", "text/html; charset=utf-8") c := appengine.NewContext(r) u := user.Current(c) if u == nil { fmt.Fprintf(w, "| Sign in using ") for _, p := range directProviders { url, err := user.LoginURLFederated(c, "/openIdLogin", p.ProviderId) if err != nil { fmt.Fprintf(w, "hoge", err) } fmt.Fprintf(w, `<a href="%s">%s</a> | `, url, p.ProviderName) } for _, p := range usernameProviders { pid := fmt.Sprintf(p.ProviderId, "nashi") url, err := user.LoginURLFederated(c, "/openIdLogin", pid) if err != nil { fmt.Fprintf(w, "hoge2", err) } fmt.Fprintf(w, `<a href="%s">%s</a> | `, url, p.ProviderName) } return } url, _ := user.LogoutURL(c, "/openIdLogin") fmt.Fprintf(w, `Welcome, %s! Provider:%s Id:%s (<a href="%s">sign out</a>)`, u.Email, u.AuthDomain, u.FederatedIdentity, url) }
func headerNotLoggedIn(w http.ResponseWriter, r *http.Request, c appengine.Context) { fmt.Fprintf(w, "Sign in with any of the following: ") providers := []struct{ name, url string }{ {"Google", "www.google.com/accounts/o8/id"}, {"Yahoo", "yahoo.com"}, {"MyOpenID", "myopenid.com"}, } for _, provider := range providers { login_url, err := user.LoginURLFederated(c, r.URL.String(), provider.url) if err != nil { continue } fmt.Fprintf(w, "[<a href='%s'>%s</a>]", login_url, provider.name) } }
func welcome(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-type", "text/html; charset=utf-8") c := appengine.NewContext(r) u := user.Current(c) if u == nil { url, err := user.LoginURLFederated(c, "/", "https://www.google.com/accounts/o8/id") if err != nil { fmt.Fprintf(w, "hoge", err) } fmt.Fprintf(w, `<a href="%s">Sign in or register using twitter</a>`, url) return } url, _ := user.LogoutURL(c, "/") fmt.Fprintf(w, `Welcome, %s! %s@%s (<a href="%s">sign out</a>)`, u.Email, u.AuthDomain, u.FederatedIdentity, url) }
func login(w http.ResponseWriter, r *http.Request) { providers := map[string]string{ "Google": "www.google.com/accounts/o8/id", "Yahoo": "yahoo.com", "MySpace": "myspace.com", "AOL": "aol.com", "MyOpenID": "myopenid.com", } c := appengine.NewContext(r) fmt.Fprintf(w, "Hey you there sigin in at: ") for name, url := range providers { login_url, err := user.LoginURLFederated(c, "/", url) if err != nil { http.Error(w, err.String(), http.StatusInternalServerError) return } fmt.Fprintf(w, "[<a href='%s'>%s</a>]", login_url, name) } }
func openIdHandler(c appengine.Context, w http.ResponseWriter, r *http.Request) { u := gaeuser.Current(c) if u != nil && u.ID != "" { err := fmt.Errorf("This page is not supposed to be accessible by logged-in users") WriteError(w, http.StatusForbidden, err) return } provider := r.FormValue("provider") if _, err := url.Parse(provider); err != nil { err := fmt.Errorf("OpenID provider name should be a valid url") WriteError(w, http.StatusBadRequest, err) return } url, err := gaeuser.LoginURLFederated(c, "/-/me", provider) if err != nil { gonuts.LogError(c, err) return } http.Redirect(w, r, url, http.StatusSeeOther) }