initial commit
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/handlers"
|
||||
"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, svc *services.Service, repo *repositories.OAuthRepository) *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", 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", 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)
|
||||
}
|
||||
|
||||
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 clientAuthFromRequest(c *gin.Context) services.ClientAuth {
|
||||
if id, secret, ok := c.Request.BasicAuth(); ok {
|
||||
return services.ClientAuth{ClientID: id, ClientSecret: secret}
|
||||
}
|
||||
return services.ClientAuth{
|
||||
ClientID: c.PostForm("client_id"),
|
||||
ClientSecret: c.PostForm("client_secret"),
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user