teldrive/services/auth.service.go

307 lines
8.6 KiB
Go
Raw Normal View History

2023-08-12 19:21:42 +08:00
package services
import (
"bytes"
2023-08-13 04:15:19 +08:00
"context"
2023-08-12 19:21:42 +08:00
"encoding/base64"
"encoding/binary"
2023-08-15 03:36:24 +08:00
"encoding/json"
2023-08-12 19:21:42 +08:00
"errors"
2023-08-15 03:36:24 +08:00
"fmt"
"log"
2023-08-12 19:21:42 +08:00
"math/big"
"net"
"net/http"
2023-08-13 04:15:19 +08:00
"strconv"
2023-08-12 19:21:42 +08:00
"time"
2023-08-16 05:53:02 +08:00
"github.com/divyam234/teldrive/models"
"github.com/divyam234/teldrive/schemas"
"github.com/divyam234/teldrive/types"
"github.com/divyam234/teldrive/utils"
"github.com/divyam234/teldrive/utils/auth"
2023-08-12 19:21:42 +08:00
"github.com/gin-gonic/gin"
"github.com/go-jose/go-jose/v3/jwt"
2023-08-15 03:36:24 +08:00
"github.com/gorilla/websocket"
"github.com/gotd/td/session"
tgauth "github.com/gotd/td/telegram/auth"
"github.com/gotd/td/telegram/auth/qrlogin"
"github.com/gotd/td/tg"
2023-08-14 04:58:06 +08:00
"gorm.io/gorm"
2023-08-12 19:21:42 +08:00
)
type AuthService struct {
2023-08-14 04:58:06 +08:00
Db *gorm.DB
2023-08-12 19:21:42 +08:00
SessionMaxAge int
}
2023-08-15 03:36:24 +08:00
type SessionData struct {
Version int
Data session.Data
}
type SocketMessage struct {
AuthType string `json:"authType"`
Message string `json:"message"`
PhoneNo string `json:"phoneNo,omitempty"`
PhoneCodeHash string `json:"phoneCodeHash,omitempty"`
PhoneCode string `json:"phoneCode,omitempty"`
}
2023-08-12 19:21:42 +08:00
func IP4toInt(IPv4Address net.IP) int64 {
IPv4Int := big.NewInt(0)
IPv4Int.SetBytes(IPv4Address.To4())
return IPv4Int.Int64()
}
func Pack32BinaryIP4(ip4Address string) []byte {
ipv4Decimal := IP4toInt(net.ParseIP(ip4Address))
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint32(ipv4Decimal))
return buf.Bytes()
}
2023-08-15 03:36:24 +08:00
func generateTgSession(dcID int, authKey []byte, port int) string {
dcMaps := map[int]string{
1: "149.154.175.53",
2: "149.154.167.51",
3: "149.154.175.100",
4: "149.154.167.91",
5: "91.108.56.130",
}
2023-08-12 19:21:42 +08:00
dcIDByte := byte(dcID)
2023-08-15 03:36:24 +08:00
serverAddressBytes := Pack32BinaryIP4(dcMaps[dcID])
2023-08-12 19:21:42 +08:00
portByte := make([]byte, 2)
binary.BigEndian.PutUint16(portByte, uint16(port))
packet := make([]byte, 0)
packet = append(packet, dcIDByte)
packet = append(packet, serverAddressBytes...)
packet = append(packet, portByte...)
packet = append(packet, authKey...)
base64Encoded := base64.URLEncoding.EncodeToString(packet)
return "1" + base64Encoded
}
func GetUserSessionCookieName(c *gin.Context) string {
isHttps := c.Request.URL.Scheme == "https"
var cookieName string
if isHttps {
cookieName = "__Secure-user-session"
} else {
cookieName = "user-session"
}
return cookieName
}
2023-08-14 04:58:06 +08:00
func (as *AuthService) LogIn(c *gin.Context) (*schemas.Message, *types.AppError) {
2023-08-12 19:21:42 +08:00
var session types.TgSession
if err := c.ShouldBindJSON(&session); err != nil {
2023-08-14 04:58:06 +08:00
return nil, &types.AppError{Error: errors.New("invalid request payload"), Code: http.StatusBadRequest}
2023-08-12 19:21:42 +08:00
}
now := time.Now().UTC()
jwtClaims := &types.JWTClaims{Claims: jwt.Claims{
2023-08-15 03:36:24 +08:00
Subject: strconv.Itoa(session.UserID),
2023-08-12 19:21:42 +08:00
IssuedAt: jwt.NewNumericDate(now),
Expiry: jwt.NewNumericDate(now.Add(time.Duration(as.SessionMaxAge) * time.Second)),
2023-08-15 03:36:24 +08:00
}, TgSession: session.Sesssion,
Name: session.Name,
UserName: session.UserName,
Bot: session.Bot,
IsPremium: session.IsPremium,
}
2023-08-12 19:21:42 +08:00
jweToken, err := auth.Encode(jwtClaims)
if err != nil {
2023-08-14 04:58:06 +08:00
return nil, &types.AppError{Error: err, Code: http.StatusBadRequest}
2023-08-12 19:21:42 +08:00
}
2023-08-14 04:58:06 +08:00
user := models.User{
2023-08-15 03:36:24 +08:00
UserId: session.UserID,
2023-08-14 04:58:06 +08:00
Name: session.Name,
UserName: session.UserName,
IsPremium: session.IsPremium,
2023-08-15 03:36:24 +08:00
TgSession: session.Sesssion,
2023-08-14 04:58:06 +08:00
}
2023-08-17 00:24:43 +08:00
var result []models.User
if err := as.Db.Model(&models.User{}).Where("user_id = ?", session.UserID).Find(&result).Error; err != nil {
2023-08-14 04:58:06 +08:00
return nil, &types.AppError{Error: errors.New("failed to create or update user"), Code: http.StatusInternalServerError}
}
2023-08-17 00:24:43 +08:00
if len(result) == 0 {
if err := as.Db.Create(&user).Error; err != nil {
return nil, &types.AppError{Error: errors.New("failed to create or update user"), Code: http.StatusInternalServerError}
}
2023-08-17 01:10:41 +08:00
//Create root folder on first login
file := &models.File{
Name: "root",
Type: "folder",
MimeType: "drive/folder",
Path: "/",
Depth: utils.IntPointer(0),
UserID: session.UserID,
Status: "active",
ParentID: "root",
}
if err := as.Db.Create(file).Error; err != nil {
return nil, &types.AppError{Error: errors.New("failed to create or update user"), Code: http.StatusInternalServerError}
}
2023-08-17 00:24:43 +08:00
} else {
if err := as.Db.Model(&models.User{}).Where("user_id = ?", session.UserID).Update("tg_session", session.Sesssion).Error; err != nil {
return nil, &types.AppError{Error: errors.New("failed to create or update user"), Code: http.StatusInternalServerError}
}
}
2023-08-16 05:02:08 +08:00
isHttps := c.Request.URL.Scheme == "https"
c.SetSameSite(2)
c.SetCookie(GetUserSessionCookieName(c), jweToken, as.SessionMaxAge, "/", c.Request.Host, isHttps, true)
2023-08-14 04:58:06 +08:00
return &schemas.Message{Status: true, Message: "login success"}, nil
2023-08-12 19:21:42 +08:00
}
func (as *AuthService) GetSession(c *gin.Context) *types.Session {
cookie, err := c.Request.Cookie(GetUserSessionCookieName(c))
if err != nil {
return nil
}
jwePayload, err := auth.Decode(cookie.Value)
if err != nil {
return nil
}
now := time.Now().UTC()
newExpires := now.Add(time.Duration(as.SessionMaxAge) * time.Second)
session := &types.Session{Name: jwePayload.Name, UserName: jwePayload.UserName, Expires: newExpires.Format(time.RFC3339)}
jwePayload.IssuedAt = jwt.NewNumericDate(now)
jwePayload.Expiry = jwt.NewNumericDate(newExpires)
jweToken, err := auth.Encode(jwePayload)
if err != nil {
return nil
}
2023-08-16 05:02:08 +08:00
isHttps := c.Request.URL.Scheme == "https"
c.SetSameSite(2)
c.SetCookie(GetUserSessionCookieName(c), jweToken, as.SessionMaxAge, "/", c.Request.Host, isHttps, true)
2023-08-12 19:21:42 +08:00
return session
}
2023-08-13 04:15:19 +08:00
2023-08-14 04:58:06 +08:00
func (as *AuthService) Logout(c *gin.Context) (*schemas.Message, *types.AppError) {
2023-08-13 04:15:19 +08:00
val, _ := c.Get("jwtUser")
jwtUser := val.(*types.JWTClaims)
userId, _ := strconv.Atoi(jwtUser.Subject)
2023-08-16 05:02:08 +08:00
tgClient, stop, err := utils.GetAuthClient(jwtUser.TgSession, userId)
2023-08-13 04:15:19 +08:00
if err != nil {
2023-08-14 04:58:06 +08:00
return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError}
2023-08-13 04:15:19 +08:00
}
2023-08-15 03:36:24 +08:00
tgClient.Tg.API().AuthLogOut(c)
2023-08-13 04:15:19 +08:00
utils.StopClient(stop, userId)
2023-08-16 05:02:08 +08:00
isHttps := c.Request.URL.Scheme == "https"
c.SetSameSite(2)
c.SetCookie(GetUserSessionCookieName(c), "", -1, "/", c.Request.Host, isHttps, true)
2023-08-14 04:58:06 +08:00
return &schemas.Message{Status: true, Message: "logout success"}, nil
2023-08-13 04:15:19 +08:00
}
2023-08-15 03:36:24 +08:00
func prepareSession(user *tg.User, data *session.Data) *types.TgSession {
sessionString := generateTgSession(data.DC, data.AuthKey, 443)
session := &types.TgSession{
Sesssion: sessionString,
UserID: int(user.ID),
Bot: user.Bot,
UserName: user.Username,
Name: fmt.Sprintf("%s %s", user.FirstName, user.LastName),
IsPremium: user.Premium,
}
return session
}
func (as *AuthService) HandleQrCodeLogin(c *gin.Context) {
upgrader := websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
log.Println(err)
return
}
defer conn.Close()
dispatcher := tg.NewUpdateDispatcher()
loggedIn := qrlogin.OnLoginToken(dispatcher)
sessionStorage := &session.StorageMemory{}
tgClient, stop, _ := utils.GetNonAuthClient(dispatcher, sessionStorage)
defer stop()
for {
message := &SocketMessage{}
err := conn.ReadJSON(message)
if message.AuthType == "qr" {
2023-08-17 00:24:43 +08:00
go func() {
authorization, err := tgClient.QR().Auth(c, loggedIn, func(ctx context.Context, token qrlogin.Token) error {
conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": map[string]string{"token": token.URL()}})
return nil
})
if err != nil {
return
}
user, ok := authorization.User.AsNotEmpty()
if !ok {
return
}
res, _ := sessionStorage.LoadSession(c)
sessionData := &SessionData{}
json.Unmarshal(res, sessionData)
session := prepareSession(user, &sessionData.Data)
conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": session, "message": "success"})
}()
2023-08-15 03:36:24 +08:00
}
if message.AuthType == "phone" && message.Message == "sendcode" {
res, err := tgClient.Auth().SendCode(c, message.PhoneNo, tgauth.SendCodeOptions{})
if err != nil {
2023-08-17 00:24:43 +08:00
return
2023-08-15 03:36:24 +08:00
}
code := res.(*tg.AuthSentCode)
conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": map[string]string{"phoneCodeHash": code.PhoneCodeHash}})
}
if message.AuthType == "phone" && message.Message == "signin" {
auth, err := tgClient.Auth().SignIn(c, message.PhoneNo, message.PhoneCode, message.PhoneCodeHash)
if err != nil {
2023-08-17 00:24:43 +08:00
return
2023-08-15 03:36:24 +08:00
}
user, ok := auth.User.AsNotEmpty()
if !ok {
2023-08-17 00:24:43 +08:00
return
2023-08-15 03:36:24 +08:00
}
res, _ := sessionStorage.LoadSession(c)
sessionData := &SessionData{}
json.Unmarshal(res, sessionData)
session := prepareSession(user, &sessionData.Data)
conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": session, "message": "success"})
}
if err != nil {
log.Println(err)
return
}
}
}