mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-05-08 13:28:12 +00:00
fix: context controller
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/tinyauthapp/tinyauth/internal/utils"
|
"github.com/tinyauthapp/tinyauth/internal/model"
|
||||||
"github.com/tinyauthapp/tinyauth/internal/utils/tlog"
|
"github.com/tinyauthapp/tinyauth/internal/utils/tlog"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -19,7 +19,7 @@ type UserContextResponse struct {
|
|||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Provider string `json:"provider"`
|
Provider string `json:"provider"`
|
||||||
OAuth bool `json:"oauth"`
|
OAuth bool `json:"oauth"`
|
||||||
TotpPending bool `json:"totpPending"`
|
TOTPPending bool `json:"totpPending"`
|
||||||
OAuthName string `json:"oauthName"`
|
OAuthName string `json:"oauthName"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,28 +76,29 @@ func (controller *ContextController) SetupRoutes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (controller *ContextController) userContextHandler(c *gin.Context) {
|
func (controller *ContextController) userContextHandler(c *gin.Context) {
|
||||||
context, err := utils.GetContext(c)
|
context, err := new(model.UserContext).NewFromGin(c)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
tlog.App.Debug().Err(err).Msg("No user context found in request")
|
||||||
|
c.JSON(200, UserContextResponse{
|
||||||
|
Status: 401,
|
||||||
|
Message: "Unauthorized",
|
||||||
|
IsLoggedIn: false,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
userContext := UserContextResponse{
|
userContext := UserContextResponse{
|
||||||
Status: 200,
|
Status: 200,
|
||||||
Message: "Success",
|
Message: "Success",
|
||||||
IsLoggedIn: context.IsLoggedIn,
|
IsLoggedIn: context.Authenticated,
|
||||||
Username: context.Username,
|
Username: context.GetUsername(),
|
||||||
Name: context.Name,
|
Name: context.GetName(),
|
||||||
Email: context.Email,
|
Email: context.GetEmail(),
|
||||||
Provider: context.Provider,
|
Provider: context.ProviderName(),
|
||||||
OAuth: context.OAuth,
|
OAuth: context.IsOAuth(),
|
||||||
TotpPending: context.TotpPending,
|
TOTPPending: context.TOTPPending(),
|
||||||
OAuthName: context.OAuthName,
|
OAuthName: context.OAuthName(),
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
tlog.App.Debug().Err(err).Msg("No user context found in request")
|
|
||||||
userContext.Status = 401
|
|
||||||
userContext.Message = "Unauthorized"
|
|
||||||
userContext.IsLoggedIn = false
|
|
||||||
c.JSON(200, userContext)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(200, userContext)
|
c.JSON(200, userContext)
|
||||||
|
|||||||
@@ -177,3 +177,30 @@ func (c *UserContext) GetName() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *UserContext) ProviderName() string {
|
||||||
|
switch c.Provider {
|
||||||
|
case ProviderBasicAuth, ProviderLocal:
|
||||||
|
return "local"
|
||||||
|
case ProviderLDAP:
|
||||||
|
return "ldap"
|
||||||
|
case ProviderOAuth:
|
||||||
|
return c.OAuth.DisplayName // compatability
|
||||||
|
default:
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *UserContext) TOTPPending() bool {
|
||||||
|
if c.Provider == ProviderLocal {
|
||||||
|
return c.Local.TOTPPending
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *UserContext) OAuthName() string {
|
||||||
|
if c.Provider == ProviderOAuth {
|
||||||
|
return c.OAuth.DisplayName
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user