From 77120ad757c308b6229daae8093af6b9b50da543 Mon Sep 17 00:00:00 2001 From: wys Date: Sat, 22 Nov 2025 18:07:32 +0800 Subject: [PATCH] [add] Simulate Telegram OAuth2 login --- telegram/telegram.go | 16 ++++++++++++++++ token.go | 6 ++++++ 2 files changed, 22 insertions(+) create mode 100644 telegram/telegram.go diff --git a/telegram/telegram.go b/telegram/telegram.go new file mode 100644 index 000000000..7cc0ba682 --- /dev/null +++ b/telegram/telegram.go @@ -0,0 +1,16 @@ +package telegram + +import "golang.org/x/oauth2" + +var Endpoint = oauth2.Endpoint{ + AuthURL: "https://oauth.telegram.org/auth", +} + +func SetTelegramAuthStyle(botID string, originDomain string, redirectURL string) []oauth2.AuthCodeOption { + return []oauth2.AuthCodeOption{ + oauth2.SetAuthURLParam("bot_id", botID), + oauth2.SetAuthURLParam("origin", originDomain), + oauth2.SetAuthURLParam("return_to", redirectURL), + oauth2.SetAuthURLParam("request_access", "write&embed=0"), + } +} diff --git a/token.go b/token.go index e995eebb5..f3607e39e 100644 --- a/token.go +++ b/token.go @@ -6,6 +6,7 @@ package oauth2 import ( "context" + "errors" "fmt" "net/http" "net/url" @@ -172,6 +173,11 @@ func tokenFromInternal(t *internal.Token) *Token { // This token is then mapped from *internal.Token into an *oauth2.Token which is returned along // with an error. func retrieveToken(ctx context.Context, c *Config, v url.Values) (*Token, error) { + + if c.Endpoint.TokenURL == "" { + return nil, errors.New("oauth2: no token endpoint") + } + tk, err := internal.RetrieveToken(ctx, c.ClientID, c.ClientSecret, c.Endpoint.TokenURL, v, internal.AuthStyle(c.Endpoint.AuthStyle), c.authStyleCache.Get()) if err != nil { if rErr, ok := err.(*internal.RetrieveError); ok {