Created
October 8, 2012 13:00
-
-
Save shanna/3852389 to your computer and use it in GitHub Desktop.
Persona Verification in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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