Using AppEngine / Go application APIs with OAuth: sample code, workflow, any help?

While I am very experienced in the AppEngine / Python runtime, I am new to the Go runtime. My first application is close to being ready for deployment, but I still need to give the user the ability to log in. I hope to use OpenID, as I would prefer not to require the user to have a Google identifier.

However, it seems that there are almost no working examples there, and the explication of the AppEngine documentation does not contain the contents of the function that I need to implement:

func init() { http.HandleFunc("/_ah/login_required", openIdHandler) } func openIdHandler(w http.ResponseWriter, r *http.Request) { // ... } 

What is included in openIdHandler func?

I understand that I need to provide a page that allows the user to select one of the many OpenId providers and enter their identifier for this system. I just don’t know what to do after that. What is a workflow? Does anyone know any example code that I can look at to get a general idea of ​​what I should do and what data I should process? All my well-honed google-fu brought me to nothing.

To be clear, I do not want to interact with any services provided by these OpenId providers; I do not want to create Tweets or Buzz. I don't need access to contacts, documents, wall posts, or anything else. I just need an authenticated credenital that I can use in my application to restrict users only to their own data.

+6
source share
1 answer

If I understand you well, you need , not . I rewrote the python ( Federated Login and Logout ) example for go-lang. I hope for this help.

 package gae_go_openid_demo import ( "fmt" "os" "http" "appengine" "appengine/user" ) func init() { http.HandleFunc("/", hello) http.HandleFunc("/_ah/login_required", openIdHandler) } func hello(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) u := user.Current(c) if u != nil { url, err := user.LogoutURL(c, "/") check(err); fmt.Fprintf(w, "Hello, %s! (<a href='%s'>Sign out</a>)", u, url) } else { fmt.Fprintf(w, "Please, <a href='/_ah/login_required'>login</a>.") } } func openIdHandler(w http.ResponseWriter, r *http.Request) { providers := map[string]string { "Google" : "www.google.com/accounts/o8/id", // shorter alternative: "Gmail.com" "Yahoo" : "yahoo.com", "MySpace" : "myspace.com", "AOL" : "aol.com", "MyOpenID" : "myopenid.com", // add more here } c := appengine.NewContext(r) fmt.Fprintf(w, "Sign in at: ") for name, url := range providers { login_url, err := user.LoginURLFederated(c, "/", url) check(err); fmt.Fprintf(w, "[<a href='%s'>%s</a>]", login_url, name) } } // check aborts the current execution if err is non-nil. func check(err os.Error) { if err != nil { panic(err) } } 
+10
source

Source: https://habr.com/ru/post/900662/


All Articles