Skip to content

Instantly share code, notes, and snippets.

@shanna
Created October 8, 2012 13:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanna/3852389 to your computer and use it in GitHub Desktop.
Save shanna/3852389 to your computer and use it in GitHub Desktop.
Persona Verification in Go
package persona
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
type Auth struct {
Status, Email, Audience, Issuer, Reason string
Expires uint64
}
func (a *Auth) Authenticated() bool {
return a.Status == "okay"
}
func failure(err error, auth *Auth) bool {
if err != nil {
auth.Status = "failure"
auth.Reason = err.Error()
return true
}
return false
}
func Authenticate(assertion, audience string) (auth Auth) {
// Post to verify.
response, err := http.PostForm(
"https://verifier.login.persona.org/verify",
url.Values{"assertion": {assertion}, "audience": {audience}},
)
if failure(err, &auth) {
return
}
// Read response.
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if failure(err, &auth) {
return
}
// Read json into Auth struct.
err = json.Unmarshal(body, &auth)
failure(err, &auth)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment