Download this file
| 1 | package setup |
| 2 | |
| 3 | import ( |
| 4 | "crypto/hmac" |
| 5 | "crypto/sha256" |
| 6 | "encoding/base64" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | // PairingConfirmation binds the typed-code inspection to its verified person. |
| 13 | func PairingConfirmation(key, person, code string, now time.Time) string { |
| 14 | if key == "" || person == "" || !validUserCode(code) { |
| 15 | return "" |
| 16 | } |
| 17 | expires := strconv.FormatInt(now.Add(5*time.Minute).Unix(), 10) |
| 18 | return expires + "." + pairingMAC(key, person, code, expires) |
| 19 | } |
| 20 | func pairingMAC(key, person, code, expires string) string { |
| 21 | h := hmac.New(sha256.New, []byte(key)) |
| 22 | h.Write([]byte("equipo-pair-confirm\x00" + person + "\x00" + code + "\x00" + expires)) |
| 23 | return base64.RawURLEncoding.EncodeToString(h.Sum(nil)) |
| 24 | } |
| 25 | func ConfirmPairing(key, person, code, proof string, now time.Time) bool { |
| 26 | parts := strings.Split(proof, ".") |
| 27 | if len(parts) != 2 || key == "" || !validUserCode(code) { |
| 28 | return false |
| 29 | } |
| 30 | expires, e := strconv.ParseInt(parts[0], 10, 64) |
| 31 | return e == nil && now.Unix() < expires && expires <= now.Add(5*time.Minute).Unix() && hmac.Equal([]byte(parts[1]), []byte(pairingMAC(key, person, code, parts[0]))) |
| 32 | } |
| 33 | |