Files
mesh/token.go
T

211 lines
5.0 KiB
Go

// Code generated by zi2go from token.zi. DO NOT EDIT.
package main
import strings "strings"
// #import codec
// #import vec
// #import identity
// #import hmac_sha256_go
// #import constant_time
type AuthTokenResult struct {
Value string
Error string
}
type ParsedInteger struct {
Value int64
IsOk bool
}
func Token_DecimalText(value int64) AuthTokenResult {
var result AuthTokenResult = AuthTokenResult{}
result.Error = "token allocation failed"
remaining := uint64(value)
negative := value < 0
if negative {
remaining = 0 - remaining
}
var digits [20]uint8
var start int64 = 20
for true {
start--
digits[start] = uint8((48 + (remaining % 10)))
remaining /= 10
if remaining == 0 {
break
}
}
var output __type_838711075b90279a = __type_838711075b90279a{}
var value_0 *__type_838711075b90279a = &output
if negative {
var value_1 uint8 = 45
var value_2 bool = false
output.Data = append(output.Data, value_1)
output.Count = int64(len(output.Data))
output.Capacity = int64(cap(output.Data))
value_2 = true
if !value_2 {
var value_3 AuthTokenResult = result
value_0.Data = nil
value_0.Capacity = 0
value_0.Count = 0
return value_3
}
}
for start < 20 {
var value_4 uint8 = digits[start]
var value_5 bool = false
output.Data = append(output.Data, value_4)
output.Count = int64(len(output.Data))
output.Capacity = int64(cap(output.Data))
value_5 = true
if !value_5 {
var value_6 AuthTokenResult = result
value_0.Data = nil
value_0.Capacity = 0
value_0.Count = 0
return value_6
}
start++
}
var value_7 string = ""
value_7 = string(output.Data[:output.Count])
output.Data = nil
output.Capacity = 0
output.Count = 0
result.Value = value_7
result.Error = ""
var value_8 AuthTokenResult = result
value_0.Data = nil
value_0.Capacity = 0
value_0.Count = 0
return value_8
}
func Token_ParseInteger(text string) ParsedInteger {
var result ParsedInteger = ParsedInteger{}
if int64(len(text)) == 0 {
return result
}
negative := text[0] == 45
var start int64 = 0
if negative || text[0] == 43 {
start = 1
}
if start == int64(len(text)) {
return result
}
var limit uint64 = 9223372036854775807
if negative {
limit++
}
var value uint64 = 0
for start < int64(len(text)) {
byte := text[start]
if byte < 48 || byte > 57 {
return result
}
digit := uint64((byte - 48))
if value > (limit-digit)/10 {
return result
}
value = (value * 10) + digit
start++
}
result.Value = int64(value)
if negative {
result.Value = 0 - result.Value
}
result.IsOk = true
return result
}
func Token_IssueAuthToken(secret []uint8, userID string, expiresAt int64) AuthTokenResult {
var result AuthTokenResult = AuthTokenResult{}
var value_0 bool = int64(len(secret)) == 0
var value_1 bool = value_0
if !value_1 {
var value_2 bool = Identity_ValidUserID(userID)
value_1 = !value_2
}
if value_1 {
result.Error = "invalid token input"
return result
}
expiry := Token_DecimalText(expiresAt)
if expiry.Error != "" {
return expiry
}
parts := [4]string{"v1", userID, expiry.Value, ""}
_ = parts
payload := strings.Join(parts[0:4:4], "\n")
mac := HmacSha256Go_HMACSHA256(secret, payload)
if mac.Error != "" {
result.Error = mac.Error
return result
}
encodedPayload := Codec_EncodeBase64(payload, true, false)
encodedMAC := Codec_EncodeBase64(mac.Value, true, false)
if encodedPayload.Error != "" || encodedMAC.Error != "" {
result.Error = "token allocation failed"
return result
}
tokenParts := [2]string{encodedPayload.Value, encodedMAC.Value}
_ = tokenParts
var value_3 string = strings.Join(tokenParts[0:2:2], ".")
result.Value = value_3
return result
}
func Token_VerifyAuthToken(secret []uint8, token string, now int64) AuthTokenResult {
var result AuthTokenResult = AuthTokenResult{}
parts := strings.Split(token, ".")
if int64(len(parts)) != 2 || int64(len(secret)) == 0 {
result.Error = "invalid token"
return result
}
payload := Codec_DecodeBase64(parts[0], true, false)
if payload.Error != "" {
result.Error = "invalid token payload"
return result
}
signature := Codec_DecodeBase64(parts[1], true, false)
if signature.Error != "" {
result.Error = "invalid token signature"
return result
}
mac := HmacSha256Go_HMACSHA256(secret, payload.Value)
if mac.Error != "" {
result.Error = mac.Error
return result
}
var value_0 bool = ConstantTime_ConstantTimeTextEqual(signature.Value, mac.Value)
if !value_0 {
result.Error = "invalid token signature"
return result
}
fields := strings.Split(payload.Value, "\n")
if int64(len(fields)) < 4 {
result.Error = "invalid token payload"
return result
}
var value_1 bool = fields[0] != "v1"
var value_2 bool = value_1
if !value_2 {
var value_3 bool = Identity_ValidUserID(fields[1])
value_2 = !value_3
}
if value_2 {
result.Error = "invalid token payload"
return result
}
expiresAt := Token_ParseInteger(fields[2])
if !expiresAt.IsOk || now > expiresAt.Value {
result.Error = "token expired"
return result
}
result.Value = fields[1]
return result
}