photoprism/pkg/rnd/token.go
Michael Mayer 467f7b1585 OAuth2: Add Client Credentials Authentication #213 #782 #808 #3730 #3943
This adds standard OAuth2 client credentials and bearer token support as
well as scope-based authorization checks for REST API clients. Note that
this initial implementation should not be used in production and that
the access token limit has not been implemented yet.

Signed-off-by: Michael Mayer <michael@photoprism.app>
2023-12-12 18:42:50 +01:00

40 lines
921 B
Go

package rnd
import (
"crypto/rand"
"math/big"
)
const CharsetBase36 = "abcdefghijklmnopqrstuvwxyz0123456789"
const CharsetBase62 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// Base36 generates a random token containing lowercase letters and numbers.
func Base36(length int) string {
return Charset(length, CharsetBase36)
}
// Base62 generates a random token containing upper and lower case letters as well as numbers.
func Base62(length int) string {
return Charset(length, CharsetBase62)
}
// Charset generates a random token with the specified length and charset.
func Charset(length int, charset string) string {
if length < 1 {
return ""
} else if length > 4096 {
length = 4096
}
m := big.NewInt(int64(len(charset)))
b := make([]byte, length)
for i := range b {
if r, err := rand.Int(rand.Reader, m); err == nil {
b[i] = charset[r.Int64()]
}
}
return string(b)
}