Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
80 lines (66 sloc) 1.34 KB
package sso
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"net/url"
)
var (
ErrInvalid = errors.New("invalid sso")
)
type Nonce string
type SSO struct {
secret string
}
func New(secret string) *SSO {
return &SSO{
secret: secret,
}
}
func (s *SSO) Parse(sso, sig string) (Nonce, error) {
h := hmac.New(sha256.New, []byte(s.secret))
_, err := h.Write([]byte(sso))
if err != nil {
return "", err
}
sig2 := hex.EncodeToString(h.Sum(nil))
if sig != sig2 {
return "", ErrInvalid
}
qs, err := base64.StdEncoding.DecodeString(sso)
if err != nil {
return "", err
}
v, err := url.ParseQuery(string(qs))
if err != nil {
return "", err
}
return Nonce(v.Get("nonce")), nil
}
func (s *SSO) Build(nonce Nonce, user User) (url.Values, error) {
v := make(url.Values)
v.Set("nonce", string(nonce))
v.Set("email", user.Email)
v.Set("external_id", user.ExternalId)
v.Set("username", user.Username)
v.Set("name", user.Name)
p := base64.StdEncoding.EncodeToString([]byte(v.Encode()))
h := hmac.New(sha256.New, []byte(s.secret))
_, err := h.Write([]byte(p))
if err != nil {
return nil, err
}
sig := hex.EncodeToString(h.Sum(nil))
v = make(url.Values)
v.Set("sso", p)
v.Set("sig", sig)
return v, nil
}
type User struct {
Email string
ExternalId string
Username string
Name string
}