initial commit
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/models"
|
||||
"github.com/dusiburg/kotosoft-id/internal/services"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
svc *services.Service
|
||||
}
|
||||
|
||||
func NewAuthHandler(svc *services.Service) *AuthHandler {
|
||||
return &AuthHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Register(c *gin.Context) {
|
||||
var input models.UserRegisterInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.Error(models.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
id, secret, ok := c.Request.BasicAuth()
|
||||
if !ok {
|
||||
c.Error(models.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
service, err := h.svc.AuthenticateClient(c.Request.Context(), services.ClientAuth{ClientID: id, ClientSecret: secret})
|
||||
if err != nil {
|
||||
c.Error(models.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
if !service.Trusted {
|
||||
c.Error(models.ErrForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.svc.Register(c.Request.Context(), input)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, user)
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Refresh(c *gin.Context) {
|
||||
var input struct {
|
||||
RefreshToken string `json:"refresh_token" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.Error(models.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
pair, err := h.svc.RefreshTokens(c.Request.Context(), input.RefreshToken)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, pair)
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Profile(c *gin.Context) {
|
||||
userID := mustUserID(c)
|
||||
|
||||
user, err := h.svc.GetUserByID(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
c.Error(models.ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
func (h *AuthHandler) UpdateProfile(c *gin.Context) {
|
||||
userID := mustUserID(c)
|
||||
|
||||
var input models.UserUpdateInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.Error(models.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.svc.UpdateProfile(c.Request.Context(), userID, input)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
func (h *AuthHandler) DeleteProfile(c *gin.Context) {
|
||||
userID := mustUserID(c)
|
||||
token, _ := bearerToken(c)
|
||||
|
||||
if err := h.svc.DeleteProfile(c.Request.Context(), userID, token); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func mustUserID(c *gin.Context) uuid.UUID {
|
||||
return c.MustGet("user_id").(uuid.UUID)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/services"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ConsentHandler struct {
|
||||
svc *services.Service
|
||||
}
|
||||
|
||||
func NewConsentHandler(svc *services.Service) *ConsentHandler {
|
||||
return &ConsentHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *ConsentHandler) GetUserConsents(c *gin.Context) {
|
||||
userID := mustUserID(c)
|
||||
|
||||
consents, err := h.svc.GetUserConsents(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, consents)
|
||||
}
|
||||
|
||||
func (h *ConsentHandler) RevokeAppAccess(c *gin.Context) {
|
||||
userID := mustUserID(c)
|
||||
clientID := c.Param("clientId")
|
||||
|
||||
if err := h.svc.RevokeConsent(c.Request.Context(), userID, clientID); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/services"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type OAuthHandler struct {
|
||||
svc *services.Service
|
||||
frontendURL string
|
||||
}
|
||||
|
||||
func NewOAuthHandler(svc *services.Service, frontendURL string) *OAuthHandler {
|
||||
return &OAuthHandler{svc: svc, frontendURL: frontendURL}
|
||||
}
|
||||
|
||||
func (h *OAuthHandler) Authorize(c *gin.Context) {
|
||||
token, err := h.svc.CreateOAuthSession(
|
||||
c.Request.Context(),
|
||||
c.Query("client_id"),
|
||||
c.Query("redirect_uri"),
|
||||
c.Query("scope"),
|
||||
c.Query("state"),
|
||||
c.Query("code_challenge"),
|
||||
c.Query("code_challenge_method"),
|
||||
)
|
||||
if err != nil {
|
||||
oauthErrorJSON(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Redirect(http.StatusFound, h.frontendURL+"/oauth?session="+token)
|
||||
}
|
||||
|
||||
func (h *OAuthHandler) GetSession(c *gin.Context) {
|
||||
sess, err := h.svc.GetOAuthSession(c.Request.Context(), c.Param("token"))
|
||||
if err != nil {
|
||||
oauthErrorJSON(c, services.ErrInvalidGrant)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, sess)
|
||||
}
|
||||
|
||||
func (h *OAuthHandler) SessionLogin(c *gin.Context) {
|
||||
var input struct {
|
||||
Session string `json:"session" binding:"required"`
|
||||
Email string `json:"email" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
oauthErrorJSON(c, services.ErrInvalidRequest)
|
||||
return
|
||||
}
|
||||
|
||||
redirectURL, err := h.svc.SessionLogin(c.Request.Context(), input.Session, input.Email, input.Password)
|
||||
if err != nil {
|
||||
if err == services.ErrInvalidGrant {
|
||||
oauthErrorJSON(c, services.ErrInvalidGrant)
|
||||
} else {
|
||||
oauthErrorJSON(c, services.ErrAccessDenied)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if redirectURL != "" {
|
||||
c.JSON(http.StatusOK, gin.H{"redirect_url": redirectURL})
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *OAuthHandler) SessionAttach(c *gin.Context) {
|
||||
var input struct {
|
||||
Session string `json:"session" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
oauthErrorJSON(c, services.ErrInvalidRequest)
|
||||
return
|
||||
}
|
||||
|
||||
accessToken, ok := bearerToken(c)
|
||||
if !ok {
|
||||
oauthErrorJSON(c, services.ErrInvalidClient)
|
||||
return
|
||||
}
|
||||
|
||||
redirectURL, err := h.svc.SessionAttach(c.Request.Context(), input.Session, accessToken)
|
||||
if err != nil {
|
||||
oauthErrorJSON(c, services.ErrAccessDenied)
|
||||
return
|
||||
}
|
||||
|
||||
if redirectURL != "" {
|
||||
c.JSON(http.StatusOK, gin.H{"redirect_url": redirectURL})
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *OAuthHandler) SessionApprove(c *gin.Context) {
|
||||
var input struct {
|
||||
Session string `json:"session" binding:"required"`
|
||||
Allow bool `json:"allow"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
oauthErrorJSON(c, services.ErrInvalidRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if !input.Allow {
|
||||
oauthErrorJSON(c, services.ErrAccessDenied)
|
||||
return
|
||||
}
|
||||
|
||||
redirectURL, err := h.svc.SessionApprove(c.Request.Context(), input.Session)
|
||||
if err != nil {
|
||||
oauthErrorJSON(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"redirect_url": redirectURL})
|
||||
}
|
||||
|
||||
func (h *OAuthHandler) Token(c *gin.Context) {
|
||||
auth := h.extractClientAuth(c)
|
||||
|
||||
switch c.PostForm("grant_type") {
|
||||
case "authorization_code":
|
||||
pair, err := h.svc.ExchangeCode(
|
||||
c.Request.Context(), auth,
|
||||
c.PostForm("code"),
|
||||
c.PostForm("redirect_uri"),
|
||||
c.PostForm("code_verifier"),
|
||||
)
|
||||
if err != nil {
|
||||
oauthErrorJSON(c, err)
|
||||
return
|
||||
}
|
||||
pair.User = nil
|
||||
c.JSON(http.StatusOK, pair)
|
||||
|
||||
case "refresh_token":
|
||||
pair, err := h.svc.ExchangeRefreshToken(c.Request.Context(), auth, c.PostForm("refresh_token"))
|
||||
if err != nil {
|
||||
oauthErrorJSON(c, err)
|
||||
return
|
||||
}
|
||||
pair.User = nil
|
||||
c.JSON(http.StatusOK, pair)
|
||||
|
||||
default:
|
||||
oauthErrorJSON(c, services.ErrUnsupportedGrantType)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *OAuthHandler) Revoke(c *gin.Context) {
|
||||
_ = h.svc.RevokeToken(c.Request.Context(), h.extractClientAuth(c), c.PostForm("token"))
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *OAuthHandler) extractClientAuth(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 oauthErrorJSON(c *gin.Context, err error) {
|
||||
code, desc, status := oauthErrorDetails(err)
|
||||
if code == "invalid_client" {
|
||||
c.Header("WWW-Authenticate", `Basic realm="oauth"`)
|
||||
}
|
||||
c.JSON(status, gin.H{"error": code, "error_description": desc})
|
||||
}
|
||||
|
||||
func oauthErrorDetails(err error) (code, desc string, status int) {
|
||||
switch err {
|
||||
case services.ErrInvalidRequest:
|
||||
return "invalid_request", "Invalid request parameters", http.StatusBadRequest
|
||||
case services.ErrUnauthorizedClient:
|
||||
return "unauthorized_client", "Client not authorized", http.StatusBadRequest
|
||||
case services.ErrAccessDenied:
|
||||
return "access_denied", "Access denied", http.StatusForbidden
|
||||
case services.ErrInvalidClient:
|
||||
return "invalid_client", "Invalid client credentials", http.StatusUnauthorized
|
||||
case services.ErrInvalidGrant:
|
||||
return "invalid_grant", "Invalid or expired grant", http.StatusBadRequest
|
||||
case services.ErrUnsupportedGrantType:
|
||||
return "unsupported_grant_type", "Grant type not supported", http.StatusBadRequest
|
||||
default:
|
||||
return "server_error", "An unexpected error occurred", http.StatusInternalServerError
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user