-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
telegram login using tma token / mini-app init data (#189)
- Loading branch information
1 parent
66fb208
commit b6b5553
Showing
23 changed files
with
744 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: ice License 1.0 | ||
|
||
package auth | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pkg/errors" | ||
|
||
wintrauth "github.com/ice-blockchain/wintr/auth" | ||
) | ||
|
||
func NewRefresher(authClient wintrauth.Client, email, telegram ProviderRefresher) TokenRefresher { | ||
return &tokenRefresher{ | ||
authClient: authClient, | ||
platforms: map[string]ProviderRefresher{ | ||
platformEmail: email, | ||
platformTelegram: telegram, | ||
}, | ||
} | ||
} | ||
|
||
func (c *tokenRefresher) RegenerateTokens(ctx context.Context, previousRefreshToken string) (tokens *Tokens, err error) { | ||
token, err := c.authClient.ParseToken(previousRefreshToken) | ||
if err != nil { | ||
if errors.Is(err, wintrauth.ErrExpiredToken) { | ||
return nil, errors.Wrapf(ErrExpiredToken, "failed to verify due to expired token:%v", previousRefreshToken) | ||
} | ||
if errors.Is(err, wintrauth.ErrInvalidToken) { | ||
return nil, errors.Wrapf(ErrInvalidToken, "failed to verify due to invalid token:%v", previousRefreshToken) | ||
} | ||
|
||
return nil, errors.Wrapf(ErrInvalidToken, "failed to verify token:%v (token:%v)", err.Error(), previousRefreshToken) | ||
} | ||
telegramUserID := "" | ||
if len(token.Claims) > 0 { | ||
if tUserIDInterface, found := token.Claims["telegramUserID"]; found { | ||
telegramUserID = tUserIDInterface.(string) //nolint:errcheck,forcetypeassert // . | ||
} | ||
} | ||
var provider ProviderRefresher | ||
switch { | ||
case telegramUserID != "": | ||
provider = c.platforms[platformTelegram] | ||
case token.Email != "": | ||
provider = c.platforms[platformEmail] | ||
default: | ||
return nil, errors.Wrapf(ErrInvalidToken, "invalid token %v cannot detect both email and telegram", previousRefreshToken) | ||
} | ||
tokens, err = provider.RefreshToken(ctx, token) | ||
|
||
return tokens, errors.Wrapf(err, "failed to refresh tokens for %#v", token) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// SPDX-License-Identifier: ice License 1.0 | ||
|
||
package auth | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
wintrauth "github.com/ice-blockchain/wintr/auth" | ||
) | ||
|
||
type ( | ||
Tokens struct { | ||
RefreshToken string `json:"refreshToken,omitempty" example:"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2ODQzMjQ0NTYsImV4cCI6MTcxNTg2MDQ1NiwiYXVkIjoiIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIm90cCI6IjUxMzRhMzdkLWIyMWEtNGVhNi1hNzk2LTAxOGIwMjMwMmFhMCJ9.q3xa8Gwg2FVCRHLZqkSedH3aK8XBqykaIy85rRU40nM"` //nolint:lll // . | ||
AccessToken string `json:"accessToken,omitempty" example:"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2ODQzMjQ0NTYsImV4cCI6MTcxNTg2MDQ1NiwiYXVkIjoiIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIm90cCI6IjUxMzRhMzdkLWIyMWEtNGVhNi1hNzk2LTAxOGIwMjMwMmFhMCJ9.q3xa8Gwg2FVCRHLZqkSedH3aK8XBqykaIy85rRU40nM"` //nolint:lll // . | ||
} | ||
TokenRefresher interface { | ||
RegenerateTokens(ctx context.Context, prevToken string) (tokens *Tokens, err error) | ||
} | ||
ProviderRefresher interface { | ||
RefreshToken(ctx context.Context, token *wintrauth.IceToken) (*Tokens, error) | ||
} | ||
) | ||
|
||
const ( | ||
IceIDPrefix = "ice_" | ||
) | ||
|
||
var ( | ||
ErrInvalidToken = errors.New("invalid token") | ||
ErrExpiredToken = errors.New("expired token") | ||
) | ||
|
||
// Private API. | ||
type ( | ||
tokenRefresher struct { | ||
authClient wintrauth.Client | ||
platforms map[string]ProviderRefresher | ||
} | ||
) | ||
|
||
const ( | ||
platformEmail = "email" | ||
platformTelegram = "telegram" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- SPDX-License-Identifier: ice License 1.0 | ||
|
||
CREATE TABLE IF NOT EXISTS telegram_sign_ins ( | ||
created_at timestamp NOT NULL, | ||
token_issued_at timestamp, | ||
issued_token_seq BIGINT DEFAULT 0 NOT NULL, | ||
previously_issued_token_seq BIGINT DEFAULT 0 NOT NULL, | ||
telegram_user_id text NOT NULL, | ||
user_id TEXT, | ||
primary key(telegram_user_id)) | ||
WITH (FILLFACTOR = 70); | ||
CREATE INDEX IF NOT EXISTS telegram_sign_ins ON telegram_sign_ins (user_id); |
Oops, something went wrong.