2021-01-10 00:44:02 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2021-02-04 10:16:13 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/lestrrat-go/jwx/jwa"
|
|
|
|
"github.com/lestrrat-go/jwx/jwt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
|
|
"reichard.io/imagini/graph/model"
|
|
|
|
"reichard.io/imagini/internal/config"
|
|
|
|
"reichard.io/imagini/internal/db"
|
2021-01-10 00:44:02 +00:00
|
|
|
)
|
|
|
|
|
2021-01-16 22:00:17 +00:00
|
|
|
type AuthManager struct {
|
2021-02-04 10:16:13 +00:00
|
|
|
DB *db.DBManager
|
|
|
|
Config *config.Config
|
2021-01-16 22:00:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 04:56:56 +00:00
|
|
|
func NewMgr(db *db.DBManager, c *config.Config) *AuthManager {
|
2021-02-04 10:16:13 +00:00
|
|
|
return &AuthManager{
|
|
|
|
DB: db,
|
|
|
|
Config: c,
|
|
|
|
}
|
2021-01-16 22:00:17 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
func (auth *AuthManager) AuthenticateUser(user, password string) (model.User, bool) {
|
|
|
|
// Find User by Username / Email
|
|
|
|
foundUser := &model.User{Username: user}
|
|
|
|
_, err := auth.DB.User(foundUser)
|
|
|
|
|
|
|
|
// By Username
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
foundUser = &model.User{Email: user}
|
|
|
|
_, err = auth.DB.User(foundUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
// By Email
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
log.Warn("[auth] User not found: ", user)
|
|
|
|
return *foundUser, false
|
|
|
|
} else if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return *foundUser, false
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("[auth] Authenticating user: ", foundUser.Username)
|
|
|
|
|
|
|
|
// Determine Type
|
|
|
|
switch foundUser.AuthType {
|
|
|
|
case "Local":
|
|
|
|
return *foundUser, authenticateLocalUser(*foundUser, password)
|
|
|
|
case "LDAP":
|
|
|
|
return *foundUser, authenticateLDAPUser(*foundUser, password)
|
|
|
|
default:
|
|
|
|
return *foundUser, false
|
|
|
|
}
|
2021-01-18 21:16:52 +00:00
|
|
|
}
|
2021-01-18 04:56:56 +00:00
|
|
|
|
2021-02-01 23:24:09 +00:00
|
|
|
func (auth *AuthManager) ValidateJWTRefreshToken(refreshJWT string) (jwt.Token, error) {
|
2021-02-04 10:16:13 +00:00
|
|
|
byteRefreshJWT := []byte(refreshJWT)
|
|
|
|
|
|
|
|
// Acquire Relevant Device
|
|
|
|
unverifiedToken, err := jwt.ParseBytes(byteRefreshJWT)
|
|
|
|
did, ok := unverifiedToken.Get("did")
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("did does not exist")
|
|
|
|
}
|
|
|
|
deviceID, err := uuid.Parse(fmt.Sprintf("%v", did))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("did does not parse")
|
|
|
|
}
|
|
|
|
device := &model.Device{ID: deviceID.String()}
|
|
|
|
_, err = auth.DB.Device(device)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify & Validate Token
|
|
|
|
verifiedToken, err := jwt.ParseBytes(byteRefreshJWT,
|
|
|
|
jwt.WithValidate(true),
|
|
|
|
jwt.WithVerify(jwa.HS256, []byte(*device.RefreshKey)),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("failed to parse payload: ", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return verifiedToken, nil
|
2021-01-18 04:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 23:24:09 +00:00
|
|
|
func (auth *AuthManager) ValidateJWTAccessToken(accessJWT string) (jwt.Token, error) {
|
2021-02-04 10:16:13 +00:00
|
|
|
byteAccessJWT := []byte(accessJWT)
|
|
|
|
verifiedToken, err := jwt.ParseBytes(byteAccessJWT,
|
|
|
|
jwt.WithValidate(true),
|
|
|
|
jwt.WithVerify(jwa.HS256, []byte(auth.Config.JWTSecret)),
|
|
|
|
)
|
2021-02-01 23:24:09 +00:00
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-01 23:24:09 +00:00
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
return verifiedToken, nil
|
2021-01-18 04:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 03:55:35 +00:00
|
|
|
func (auth *AuthManager) CreateJWTRefreshToken(user model.User, device model.Device) (string, error) {
|
2021-02-04 10:16:13 +00:00
|
|
|
// Acquire Refresh Key
|
|
|
|
byteKey := []byte(*device.RefreshKey)
|
|
|
|
|
|
|
|
// Create New Token
|
|
|
|
tm := time.Now()
|
|
|
|
t := jwt.New()
|
|
|
|
t.Set(`did`, device.ID) // Device ID
|
|
|
|
t.Set(jwt.SubjectKey, user.ID) // User ID
|
|
|
|
t.Set(jwt.AudienceKey, `imagini`) // App ID
|
|
|
|
t.Set(jwt.IssuedAtKey, tm) // Issued At
|
|
|
|
|
|
|
|
// iOS & Android = Never Expiring Refresh Token
|
|
|
|
if device.Type != "iOS" && device.Type != "Android" {
|
|
|
|
t.Set(jwt.ExpirationKey, tm.Add(time.Hour*24)) // 1 Day Access Key
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate Token Creation
|
|
|
|
_, err := json.MarshalIndent(t, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to generate JSON: %s\n", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign Token
|
|
|
|
signed, err := jwt.Sign(t, jwa.HS256, byteKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to sign token: %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return Token
|
|
|
|
return string(signed), nil
|
2021-01-18 04:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 03:55:35 +00:00
|
|
|
func (auth *AuthManager) CreateJWTAccessToken(user model.User, device model.Device) (string, error) {
|
2021-02-04 10:16:13 +00:00
|
|
|
// Create New Token
|
|
|
|
tm := time.Now()
|
|
|
|
t := jwt.New()
|
|
|
|
t.Set(`did`, device.ID) // Device ID
|
|
|
|
t.Set(`role`, user.Role.String()) // User Role (Admin / User)
|
|
|
|
t.Set(jwt.SubjectKey, user.ID) // User ID
|
|
|
|
t.Set(jwt.AudienceKey, `imagini`) // App ID
|
|
|
|
t.Set(jwt.IssuedAtKey, tm) // Issued At
|
|
|
|
t.Set(jwt.ExpirationKey, tm.Add(time.Hour*2)) // 2 Hour Access Key
|
|
|
|
|
|
|
|
// Validate Token Creation
|
|
|
|
_, err := json.MarshalIndent(t, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to generate JSON: %s\n", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use Server Key
|
|
|
|
byteKey := []byte(auth.Config.JWTSecret)
|
|
|
|
|
|
|
|
// Sign Token
|
|
|
|
signed, err := jwt.Sign(t, jwa.HS256, byteKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to sign token: %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return Token
|
|
|
|
return string(signed), nil
|
2021-01-18 04:56:56 +00:00
|
|
|
}
|