mirror of
https://github.com/hanzo-fi/auth.git
synced 2026-10-30 20:33:21 +00:00
The base64.NewEncoder requires Close() to flush any remaining buffered bytes with proper padding. Without it, the final 1-2 bytes of the JSON payload could be silently dropped, producing truncated base64 that fails to decode — breaking the encode/decode round-trip. Found via fuzz testing with FuzzDecodeDelegatedState. Co-authored-by: Hanzo Dev <[email protected]>
32 lines
702 B
Go
32 lines
702 B
Go
package delegatedauth
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
)
|
|
|
|
type DelegatedState struct {
|
|
AuthRequestID string `json:"authRequestID"`
|
|
}
|
|
|
|
func (s DelegatedState) EncodeAsUrlParam() string {
|
|
buf := bytes.NewBufferString("")
|
|
encoder := base64.NewEncoder(base64.URLEncoding, buf)
|
|
if err := json.NewEncoder(encoder).Encode(s); err != nil {
|
|
panic(err)
|
|
}
|
|
if err := encoder.Close(); err != nil {
|
|
panic(err)
|
|
}
|
|
return buf.String()
|
|
}
|
|
|
|
func DecodeDelegatedState(v string) (*DelegatedState, error) {
|
|
ret := &DelegatedState{}
|
|
if err := json.NewDecoder(base64.NewDecoder(base64.URLEncoding, bytes.NewBufferString(v))).Decode(ret); err != nil {
|
|
return nil, err
|
|
}
|
|
return ret, nil
|
|
}
|