1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-11-04 17:30:11 +00:00
jfa-go/auth.go

151 lines
4.0 KiB
Go
Raw Normal View History

2020-07-29 21:11:28 +00:00
package main
import (
"encoding/base64"
"fmt"
"os"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/lithammer/shortuuid/v3"
2020-07-29 21:11:28 +00:00
)
func (app *appContext) webAuth() gin.HandlerFunc {
return app.authenticate
2020-07-29 21:11:28 +00:00
}
func (app *appContext) authenticate(gc *gin.Context) {
2020-07-29 21:11:28 +00:00
header := strings.SplitN(gc.Request.Header.Get("Authorization"), " ", 2)
if header[0] != "Basic" {
app.debug.Println("Invalid authentication header")
2020-07-29 21:11:28 +00:00
respond(401, "Unauthorized", gc)
return
}
auth, _ := base64.StdEncoding.DecodeString(header[1])
creds := strings.SplitN(string(auth), ":", 2)
token, err := jwt.Parse(creds[0], func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
app.debug.Printf("Invalid JWT signing method %s", token.Header["alg"])
2020-07-29 21:11:28 +00:00
return nil, fmt.Errorf("Unexpected signing method %v", token.Header["alg"])
}
return []byte(os.Getenv("JFA_SECRET")), nil
})
if err != nil {
app.debug.Printf("Auth denied: %s", err)
2020-07-29 21:11:28 +00:00
respond(401, "Unauthorized", gc)
return
}
claims, ok := token.Claims.(jwt.MapClaims)
var userId string
var jfId string
2020-07-29 21:11:28 +00:00
if ok && token.Valid {
userId = claims["id"].(string)
jfId = claims["jfid"].(string)
2020-07-29 21:11:28 +00:00
} else {
app.debug.Printf("Invalid token")
2020-07-29 21:11:28 +00:00
respond(401, "Unauthorized", gc)
return
}
match := false
for _, user := range app.users {
2020-07-29 21:11:28 +00:00
if user.UserID == userId {
match = true
}
}
if !match {
app.debug.Printf("Couldn't find user ID %s", userId)
2020-07-29 21:11:28 +00:00
respond(401, "Unauthorized", gc)
return
}
gc.Set("jfId", jfId)
gc.Set("userId", userId)
app.debug.Println("Authentication successful")
2020-07-29 21:11:28 +00:00
gc.Next()
}
func (app *appContext) GetToken(gc *gin.Context) {
app.info.Println("Token requested (login attempt)")
2020-07-29 21:11:28 +00:00
header := strings.SplitN(gc.Request.Header.Get("Authorization"), " ", 2)
if header[0] != "Basic" {
app.debug.Println("Invalid authentication header")
2020-07-29 21:11:28 +00:00
respond(401, "Unauthorized", gc)
return
}
auth, _ := base64.StdEncoding.DecodeString(header[1])
creds := strings.SplitN(string(auth), ":", 2)
match := false
var userId string
for _, user := range app.users {
2020-07-29 21:11:28 +00:00
if user.Username == creds[0] && user.Password == creds[1] {
match = true
userId = user.UserID
}
}
jfId := ""
2020-07-29 21:11:28 +00:00
if !match {
if !app.jellyfinLogin {
app.info.Println("Auth failed: Invalid username and/or password")
2020-07-29 21:11:28 +00:00
respond(401, "Unauthorized", gc)
return
}
var status int
var err error
var user map[string]interface{}
user, status, err = app.authJf.authenticate(creds[0], creds[1])
jfId = user["Id"].(string)
2020-07-29 21:11:28 +00:00
if status != 200 || err != nil {
if status == 401 {
app.info.Println("Auth failed: Invalid username and/or password")
respond(401, "Unauthorized", gc)
return
}
app.err.Printf("Auth failed: Couldn't authenticate with Jellyfin: Code %d", status)
respond(500, "Jellyfin error", gc)
2020-07-29 21:11:28 +00:00
return
} else {
if app.config.Section("ui").Key("admin_only").MustBool(true) {
if !user["Policy"].(map[string]interface{})["IsAdministrator"].(bool) {
app.debug.Printf("Auth failed: User \"%s\" isn't admin", creds[0])
respond(401, "Unauthorized", gc)
}
}
2020-07-29 21:11:28 +00:00
newuser := User{}
newuser.UserID = shortuuid.New()
2020-07-29 21:11:28 +00:00
userId = newuser.UserID
// uuid, nothing else identifiable!
app.debug.Printf("Token generated for user \"%s\"", creds[0])
app.users = append(app.users, newuser)
2020-07-29 21:11:28 +00:00
}
}
token, err := CreateToken(userId, jfId)
2020-07-29 21:11:28 +00:00
if err != nil {
respond(500, "Error generating token", gc)
}
resp := map[string]string{"token": token}
gc.JSON(200, resp)
}
func CreateToken(userId string, jfId string) (string, error) {
2020-07-29 21:11:28 +00:00
claims := jwt.MapClaims{
"valid": true,
"id": userId,
"exp": time.Now().Add(time.Minute * 20).Unix(),
"jfid": jfId,
2020-07-29 21:11:28 +00:00
}
2020-07-29 21:11:28 +00:00
tk := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
token, err := tk.SignedString([]byte(os.Getenv("JFA_SECRET")))
if err != nil {
return "", err
}
return token, nil
}
func respond(code int, message string, gc *gin.Context) {
resp := map[string]string{"error": message}
gc.JSON(code, resp)
gc.Abort()
}