🚚 Auth kit and parser of nexus userinfo token

This commit is contained in:
2024-10-31 00:15:25 +08:00
parent 2d322f070a
commit 69c6ac6581
67 changed files with 143 additions and 70 deletions

27
pkg/authkit/parser.go Normal file
View File

@ -0,0 +1,27 @@
package authkit
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
)
// GetAccountFromUserInfo returns the account from the user info
// This method will not to query the database, it will parse the token and get the subject of the userinfo token
func GetAccountFromUserInfo(info *sec.UserInfo) models.Account {
raw, _ := json.Marshal(info.Metadata)
// We assume the token is signed by the same version of service
// So directly read the data out of the metadata
var out models.Account
_ = json.Unmarshal(raw, &out)
return out
}
func ParseAccountMiddleware(c *fiber.Ctx) error {
if info, ok := c.Locals("nex_user").(*sec.UserInfo); ok {
c.Locals("user", GetAccountFromUserInfo(info))
}
return c.Next()
}

View File

@ -1,7 +1,7 @@
package database
import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"gorm.io/gorm"
)

View File

@ -2,10 +2,10 @@ package grpc
import (
"context"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/samber/lo"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"

View File

@ -3,13 +3,13 @@ package grpc
import (
"context"
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"github.com/rs/zerolog/log"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"github.com/samber/lo"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hydrogen/passport/pkg/proto"
)

View File

@ -3,10 +3,10 @@ package grpc
import (
"context"
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hydrogen/passport/pkg/proto"
"github.com/samber/lo"

View File

@ -2,10 +2,10 @@ package admin
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
)

View File

@ -1,9 +1,9 @@
package admin
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/gofiber/fiber/v2"
jsoniter "github.com/json-iterator/go"
"github.com/samber/lo"

View File

@ -1,9 +1,9 @@
package admin
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/gofiber/fiber/v2"

View File

@ -2,11 +2,11 @@ package admin
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
)

View File

@ -2,9 +2,9 @@ package admin
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/gofiber/fiber/v2"

View File

@ -2,6 +2,7 @@ package api
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"strconv"
"strings"
@ -10,7 +11,6 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
jsoniter "github.com/json-iterator/go"

View File

@ -2,9 +2,9 @@ package api
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/gofiber/fiber/v2"

View File

@ -2,9 +2,9 @@ package api
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/gofiber/fiber/v2"

View File

@ -2,9 +2,9 @@ package api
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/gofiber/fiber/v2"

View File

@ -2,9 +2,9 @@ package api
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/gofiber/fiber/v2"

View File

@ -2,10 +2,10 @@ package api
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
)

View File

@ -2,6 +2,7 @@ package api
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"strings"
"time"
@ -9,7 +10,6 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"

View File

@ -1,8 +1,8 @@
package api
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/gofiber/fiber/v2"

View File

@ -1,9 +1,9 @@
package api
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/gofiber/fiber/v2"

View File

@ -1,9 +1,9 @@
package api
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/gofiber/fiber/v2"

View File

@ -1,9 +1,9 @@
package api
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/gofiber/fiber/v2"

View File

@ -1,9 +1,9 @@
package api
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/gofiber/fiber/v2"
)

View File

@ -1,9 +1,9 @@
package api
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/gofiber/fiber/v2"

View File

@ -2,13 +2,13 @@ package api
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"strconv"
"time"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"

View File

@ -2,12 +2,12 @@ package api
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"github.com/spf13/viper"
"gorm.io/gorm"
"strings"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
)

View File

@ -1,6 +1,7 @@
package http
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"strings"
@ -56,12 +57,7 @@ func NewServer() *App {
}))
app.Use(sec.ContextMiddleware(IReader))
app.Use(func(c *fiber.Ctx) error {
if user, ok := c.Locals("nex_user").(*sec.UserInfo); ok {
c.Locals("user", user)
}
return c.Next()
})
app.Use(authkit.ParseAccountMiddleware)
admin.MapAdminAPIs(app, "/api/admin")
api.MapAPIs(app, "/api")

View File

@ -1,8 +1,8 @@
package services
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/samber/lo"
)

View File

@ -3,6 +3,7 @@ package services
import (
"context"
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"time"
@ -17,7 +18,6 @@ import (
"gorm.io/datatypes"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/google/uuid"
"github.com/samber/lo"
)

View File

@ -3,6 +3,7 @@ package services
import (
"context"
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"time"
@ -12,7 +13,6 @@ import (
jsoniter "github.com/json-iterator/go"
localCache "git.solsynth.dev/hydrogen/passport/pkg/internal/cache"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
)

View File

@ -1,8 +1,8 @@
package services
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
)
func GrantBadge(user models.Account, badge models.Badge) error {

View File

@ -1,10 +1,10 @@
package services
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"time"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/google/uuid"
"github.com/samber/lo"
)

View File

@ -1,8 +1,8 @@
package services
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
)
func GetBotCount(user uint) (int64, error) {

View File

@ -1,8 +1,8 @@
package services
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/rs/zerolog/log"
"time"
)

View File

@ -2,9 +2,9 @@ package services
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
)
func GetThirdClient(id string) (models.ThirdClient, error) {

View File

@ -1,8 +1,8 @@
package services
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/rs/zerolog/log"
)

View File

@ -2,9 +2,9 @@ package services
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
"github.com/google/uuid"
"github.com/rs/zerolog/log"

View File

@ -1,10 +1,10 @@
package services
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"time"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/golang-jwt/jwt/v5"
"github.com/spf13/viper"
)

View File

@ -3,6 +3,7 @@ package services
import (
"context"
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
@ -15,7 +16,6 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
)
func AddNotifySubscriber(user models.Account, provider, id, tk, ua string) (models.NotificationSubscriber, error) {

View File

@ -3,11 +3,11 @@ package services
import (
"context"
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"time"
localCache "git.solsynth.dev/hydrogen/passport/pkg/internal/cache"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
"github.com/eko/gocache/lib/v4/store"

View File

@ -2,8 +2,8 @@ package services
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/samber/lo"
)

View File

@ -3,9 +3,9 @@ package services
import (
"errors"
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"gorm.io/gorm"
)

View File

@ -2,9 +2,7 @@ package services
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
)
func ListAbuseReport(account uint) ([]models.AbuseReport, error) {

View File

@ -3,8 +3,8 @@ package services
import (
"errors"
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"gorm.io/gorm"
"math"
"math/rand"

View File

@ -3,6 +3,7 @@ package services
import (
"context"
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"time"
@ -10,7 +11,6 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
"github.com/eko/gocache/lib/v4/store"

View File

@ -2,6 +2,7 @@ package services
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"gorm.io/datatypes"
"time"
@ -10,7 +11,6 @@ import (
"github.com/google/uuid"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/samber/lo"
)

View File

@ -1,8 +1,8 @@
package services
import (
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
)
func GetTicket(id uint) (models.AuthTicket, error) {

View File

@ -2,12 +2,12 @@ package services
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"strconv"
"time"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/samber/lo"
"github.com/spf13/viper"
)

View File

@ -2,13 +2,13 @@ package services
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
"strings"
"time"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/google/uuid"
"github.com/spf13/viper"
)