package qrlogin
import (
"encoding/base64"
"image"
"net/url"
"time"
"github.com/go-faster/errors"
"rsc.io/qr"
)
type Token struct {
token []byte
expires time .Time
}
func ParseTokenURL (u string ) (Token , error ) {
parsed , err := url .Parse (u )
if err != nil {
return Token {}, err
}
switch {
case parsed .Scheme != "tg" :
return Token {}, errors .Errorf ("unexpected scheme %q" , parsed .Scheme )
case parsed .Host != "login" :
return Token {}, errors .Errorf ("wrong path %q" , parsed .Host )
}
q := parsed .Query ()
if q .Get ("token" ) == "" {
return Token {}, errors .New ("token is empty" )
}
token , err := base64 .URLEncoding .DecodeString (q .Get ("token" ))
if err != nil {
return Token {}, err
}
return NewToken (token , 0 ), nil
}
func NewToken (token []byte , expires int ) Token {
return Token {
token : token ,
expires : time .Unix (int64 (expires ), 0 ),
}
}
func (t Token ) Expires () time .Time {
return t .expires
}
func (t Token ) String () string {
return base64 .URLEncoding .EncodeToString (t .token )
}
func (t Token ) URL () string {
return "tg://login?token=" + base64 .URLEncoding .EncodeToString (t .token )
}
func (t Token ) Image (level qr .Level ) (image .Image , error ) {
code , err := qr .Encode (t .URL (), level )
if err != nil {
return nil , errors .Wrap (err , "encode" )
}
return code .Image (), nil
}
The pages are generated with Golds v0.6.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 @Go100and1 (reachable from the left QR code) to get the latest news of Golds .