package auth
import (
"strconv"
"strings"
"github.com/go-faster/errors"
)
var ErrInvalidBotToken = errors .New ("invalid bot token" )
const maxBotTokenLength = 80
type BotToken struct {
ID int64
Secret string
}
func (t BotToken ) String () string {
return strconv .FormatInt (t .ID , 10 ) + ":" + t .Secret
}
func ParseBotToken (s string ) (BotToken , error ) {
if len (s ) > maxBotTokenLength {
return BotToken {}, errors .Wrapf (ErrInvalidBotToken , "longer than %d characters" , maxBotTokenLength )
}
if strings .Contains (s , "/" ) {
return BotToken {}, errors .Wrap (ErrInvalidBotToken , "contains '/'" )
}
rawID , secret , ok := strings .Cut (s , ":" )
if !ok {
return BotToken {}, errors .Wrap (ErrInvalidBotToken , "no ':'" )
}
if rawID == "" || rawID [0 ] == '0' {
return BotToken {}, errors .Wrap (ErrInvalidBotToken , "bot ID is empty or has a leading zero" )
}
for _ , c := range rawID {
if c < '0' || c > '9' {
return BotToken {}, errors .Wrap (ErrInvalidBotToken , "bot ID is not a decimal number" )
}
}
id , err := strconv .ParseInt (rawID , 10 , 64 )
if err != nil || id <= 0 || id >= 1 <<54 {
return BotToken {}, errors .Wrap (ErrInvalidBotToken , "bot ID out of range" )
}
if secret == "" {
return BotToken {}, errors .Wrap (ErrInvalidBotToken , "empty secret" )
}
return BotToken {ID : id , Secret : secret }, nil
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .