Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

3.8 KB
Raw Blame
package auth

import (
	"crypto/rand"
	"crypto/sha256"
	"encoding/base64"
	"math/big"
	"net/http"
	"strconv"
	"strings"

	"github.com/ente/museum/ente/cast"

	"github.com/ente/museum/ente"
	"github.com/ente/stacktrace"

	"github.com/gin-gonic/gin"
	"golang.org/x/crypto/bcrypt"
)

const (
	PublicAccessKey      = "X-Public-Access-ID"
	PublicAnonUserKey    = "X-Public-Anon-User-ID"
	FileLinkAccessKey    = "X-Public-FileLink-Access-ID"
	CastContext          = "X-Cast-Context"
	MemoryShareAccessKey = "X-Memory-Share-Access-ID"
	AppContextKey        = "auth.app"

	LinkDeviceTokenHeader         = "X-Auth-Link-Device-Token"
	LinkDeviceTokenResponseHeader = "X-Link-Device-Token"
	LinkDeviceTokenResponseKey    = "linkDeviceToken"
)

func GenerateRandomBytes(n int) []byte {
	b := make([]byte, n)
	rand.Read(b)
	return b
}

func GenerateRandomInt(n int64) (int64, error) {
	r, err := rand.Int(rand.Reader, big.NewInt(n))
	if err != nil {
		return 0, stacktrace.Propagate(err, "")
	}
	return r.Int64(), nil
}

func GenerateURLSafeRandomString(s int) string {
	return base64.URLEncoding.EncodeToString(GenerateRandomBytes(s))
}

func HashToken(token string) [sha256.Size]byte {
	return sha256.Sum256([]byte(token))
}

func GetHashedPassword(password string) (string, error) {
	saltedBytes := []byte(password)
	hashedBytes, err := bcrypt.GenerateFromPassword(saltedBytes, bcrypt.DefaultCost)
	if err != nil {
		return "", stacktrace.Propagate(err, "")
	}

	hash := string(hashedBytes[:])
	return hash, nil
}

func CompareHashes(hash string, s string) error {
	existing := []byte(hash)
	incoming := []byte(s)
	return bcrypt.CompareHashAndPassword(existing, incoming)
}

func GetUserID(header http.Header) int64 {
	userID, _ := strconv.ParseInt(header.Get("X-Auth-User-ID"), 10, 64)
	return userID
}

func GetApp(c *gin.Context) ente.App {
	if strings.HasPrefix(c.GetHeader("X-Client-Package"), "io.ente.auth") {
		return ente.Auth
	}

	if strings.HasPrefix(c.GetHeader("X-Client-Package"), "io.ente.locker") {
		return ente.Locker
	}

	return ente.Photos
}

func GetAuthenticatedApp(c *gin.Context) (ente.App, bool) {
	app, ok := c.Get(AppContextKey)
	if !ok {
		return "", false
	}
	enteApp, ok := app.(ente.App)
	return enteApp, ok
}

func GetToken(c *gin.Context) string {
	token := c.GetHeader("X-Auth-Token")
	if token == "" {
		token = c.Query("token")
	}
	return token
}

func GetAccessToken(c *gin.Context) string {
	token := c.GetHeader("X-Auth-Access-Token")
	if token == "" {
		token = c.Query("accessToken")
	}
	return token
}

func GetCastToken(c *gin.Context) string {
	token := c.GetHeader("X-Cast-Access-Token")
	if token == "" {
		token = c.Query("castToken")
	}
	return token
}

func GetAccessTokenJWT(c *gin.Context) string {
	token := c.GetHeader("X-Auth-Access-Token-JWT")
	if token == "" {
		token = c.Query("accessTokenJWT")
	}
	return token
}

func GetLinkDeviceToken(c *gin.Context) string {
	return c.GetHeader(LinkDeviceTokenHeader)
}

func MustGetPublicAccessContext(c *gin.Context) ente.PublicAccessContext {
	return c.MustGet(PublicAccessKey).(ente.PublicAccessContext)
}

func SetPublicAnonUserID(c *gin.Context, anonID string) {
	c.Set(PublicAnonUserKey, anonID)
}

func GetPublicAnonUserID(c *gin.Context) (*string, bool) {
	value, ok := c.Get(PublicAnonUserKey)
	if !ok {
		return nil, false
	}
	if anonID, ok := value.(string); ok {
		copyID := anonID
		return &copyID, true
	}
	return nil, false
}

func MustGetFileLinkAccessContext(c *gin.Context) *ente.FileLinkAccessContext {
	return c.MustGet(FileLinkAccessKey).(*ente.FileLinkAccessContext)
}

func GetCastCtx(c *gin.Context) cast.AuthContext {
	return c.MustGet(CastContext).(cast.AuthContext)
}

func MustGetMemoryShareAccessContext(c *gin.Context) ente.MemoryShareAccessContext {
	return c.MustGet(MemoryShareAccessKey).(ente.MemoryShareAccessContext)
}