114 lines
3.3 KiB
Go
114 lines
3.3 KiB
Go
package http
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/dusiburg/kotosoft-id/internal/handlers"
|
|
"github.com/dusiburg/kotosoft-id/internal/middleware"
|
|
"github.com/dusiburg/kotosoft-id/internal/models"
|
|
"github.com/dusiburg/kotosoft-id/internal/repositories"
|
|
"github.com/dusiburg/kotosoft-id/internal/services"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func NewRouter(auth *handlers.AuthHandler, oauth *handlers.OAuthHandler, consents *handlers.ConsentHandler, positions *handlers.PositionsHandler, svc *services.Service, repo *repositories.OAuthRepository, limiter *services.RateLimiter) *gin.Engine {
|
|
r := gin.Default()
|
|
r.Use(errorMiddleware())
|
|
|
|
o := r.Group("/oauth")
|
|
{
|
|
o.GET("/authorize", oauth.Authorize)
|
|
o.GET("/session/:token", oauth.GetSession)
|
|
o.POST("/session/login", middleware.RateLimit(limiter, services.LoginRateLimit, middleware.LoginKeyFn), oauth.SessionLogin)
|
|
o.POST("/session/attach", oauth.SessionAttach)
|
|
o.POST("/session/approve", oauth.SessionApprove)
|
|
o.POST("/token", oauth.Token)
|
|
o.POST("/revoke", oauth.Revoke)
|
|
}
|
|
|
|
api := r.Group("/api/v1")
|
|
{
|
|
a := api.Group("/auth")
|
|
a.POST("/register", middleware.RateLimit(limiter, services.RegisterRateLimit, middleware.IPKeyFn("register")), auth.Register)
|
|
a.POST("/refresh", auth.Refresh)
|
|
|
|
me := api.Group("/me")
|
|
me.GET("", authMiddleware(svc, repo, "profile:read"), auth.Profile)
|
|
me.PATCH("", authMiddleware(svc, repo, "profile:edit"), auth.UpdateProfile)
|
|
me.DELETE("", authMiddleware(svc, repo, "profile:delete"), auth.DeleteProfile)
|
|
me.GET("/consents", authMiddleware(svc, repo, "profile:consent:read"), consents.GetUserConsents)
|
|
me.DELETE("/consent/:clientId", authMiddleware(svc, repo, "profile:consent:delete"), consents.RevokeAppAccess)
|
|
me.GET("/positions", authMiddleware(svc, repo, "profile:read"), positions.GetMyPositions)
|
|
|
|
admin := api.Group("/admin")
|
|
admin.GET("/positions", authMiddleware(svc, repo), positions.ListPositions)
|
|
admin.POST("/positions", authMiddleware(svc, repo), positions.UpsertPosition)
|
|
}
|
|
|
|
return r
|
|
}
|
|
|
|
func authMiddleware(svc *services.Service, repo *repositories.OAuthRepository, requiredScopes ...string) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
token, ok := bearerToken(c)
|
|
if !ok {
|
|
c.Error(models.ErrUnauthorized)
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
info, err := svc.ValidateAccessToken(c.Request.Context(), token)
|
|
if err != nil {
|
|
c.Error(models.ErrUnauthorized)
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
if len(requiredScopes) > 0 && !services.ScopesContain(info.Scopes, requiredScopes) {
|
|
c.Error(models.ErrInsufficientScope)
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
if _, err := repo.GetOAuthClientByID(c.Request.Context(), info.ClientID); err != nil {
|
|
c.Error(models.ErrForbidden)
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
c.Set("user_id", info.UserID)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func errorMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Next()
|
|
if len(c.Errors) == 0 || c.Writer.Written() {
|
|
return
|
|
}
|
|
|
|
err := c.Errors.Last().Err
|
|
if apiErr, ok := err.(*models.APIError); ok {
|
|
c.JSON(apiErr.Code, apiErr)
|
|
return
|
|
}
|
|
|
|
c.JSON(500, models.ErrInternal)
|
|
}
|
|
}
|
|
|
|
func bearerToken(c *gin.Context) (string, bool) {
|
|
return strings.CutPrefix(c.GetHeader("Authorization"), "Bearer ")
|
|
}
|
|
|
|
func UserIDFromContext(c *gin.Context) (uuid.UUID, bool) {
|
|
v, ok := c.Get("user_id")
|
|
if !ok {
|
|
return uuid.Nil, false
|
|
}
|
|
id, ok := v.(uuid.UUID)
|
|
return id, ok
|
|
}
|