Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

BrowserID example in Go #2

Merged
merged 1 commit into from

2 participants

@ryuslash

I've recreated the PHP example in Go, I thought it might be interesting to send to you.

@ozten
Collaborator

Very cool, thanks!

@ozten ozten merged commit 831891e into mozilla:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 2, 2012
  1. @ryuslash

    Add Google Go example

    ryuslash authored
This page is out of date. Refresh to see the latest.
Showing with 118 additions and 0 deletions.
  1. +7 −0 go/README.md
  2. +111 −0 go/browserid.go
View
7 go/README.md
@@ -0,0 +1,7 @@
+# Google Go Example
+
+To run this example:
+
+ go run browserid.go
+
+And go to [localhost:8080](http://localhost:8080).
View
111 go/browserid.go
@@ -0,0 +1,111 @@
+package main
+
+import (
+ "encoding/json"
+ "html/template"
+ "io"
+ "io/ioutil"
+ "log"
+ "net/http"
+ "net/url"
+)
+
+const loginform = `
+<html>
+ <head>
+ <script src="https://browserid.org/include.js"></script>
+ <script>
+ function login() {
+ navigator.id.get(function(assertion) {
+ if (assertion) {
+ var assertion_field = document.getElementById("assertion-field");
+ assertion_field.value = assertion;
+ var login_form = document.getElementById("login-form");
+ login_form.submit();
+ }
+ });
+ }
+ </script>
+ </head>
+<body>
+ <form id="login-form" method="POST">
+ <input id="assertion-field" type="hidden" name="assertion" value="">
+ </form>
+
+ <p><a href="javascript:login()">Login</a></p>
+</body>
+</html>
+`
+
+const statuspage = `
+<html>
+ <body>
+ <p>
+ {{if .Okay}}
+ Logged in as: {{ .Email }}
+ {{else}}
+ Error: {{ .Reason }}
+ {{end}}
+ </p>
+
+ <p><a href="/">Return to login page</a></p>
+ </body>
+</html>
+`
+
+type BrowserIDResponse struct {
+ Status, Email, Reason string
+}
+
+func (b BrowserIDResponse) Okay() bool {
+ return b.Status == "okay"
+}
+
+func browserIDResponseFromJson(r io.Reader) (resp BrowserIDResponse) {
+ body, err := ioutil.ReadAll(r)
+
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ err = json.Unmarshal(body, &resp)
+
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ return resp
+}
+
+func verifyAssertion(assertion string) BrowserIDResponse {
+ audience := "http://localhost:8080"
+ resp, _ := http.PostForm(
+ "https://browserid.org/verify",
+ url.Values{
+ "assertion": {assertion},
+ "audience": {audience},
+ })
+ response := browserIDResponseFromJson(resp.Body)
+ resp.Body.Close()
+
+ return response
+}
+
+func rootPageHandler(w http.ResponseWriter, r *http.Request) {
+ if r.Method == "POST" {
+ result := verifyAssertion(r.FormValue("assertion"))
+ t := template.Must(template.New("statuspage").Parse(statuspage))
+ err := t.Execute(w, result)
+
+ if err != nil {
+ log.Println("executing template: ", err)
+ }
+ } else {
+ w.Write([]byte(loginform))
+ }
+}
+
+func main() {
+ http.HandleFunc("/", rootPageHandler)
+ http.ListenAndServe(":8080", nil)
+}
Something went wrong with that request. Please try again.