Compare commits

..

3 Commits

Author SHA1 Message Date
Stavros
54c003c28f New translations en.json (Chinese Traditional) 2025-11-10 04:42:47 +02:00
Stavros
8453c48d9e feat: add log in json option 2025-11-06 18:11:43 +02:00
Stavros
2af036b38e feat: add logging for session creation 2025-11-06 16:18:01 +02:00
6 changed files with 76 additions and 20 deletions

View File

@@ -14,17 +14,17 @@
"loginOauthFailSubtitle": "無法取得 OAuth 網址", "loginOauthFailSubtitle": "無法取得 OAuth 網址",
"loginOauthSuccessTitle": "重新導向中", "loginOauthSuccessTitle": "重新導向中",
"loginOauthSuccessSubtitle": "正在將您重新導向至 OAuth 供應商", "loginOauthSuccessSubtitle": "正在將您重新導向至 OAuth 供應商",
"loginOauthAutoRedirectTitle": "OAuth Auto Redirect", "loginOauthAutoRedirectTitle": "OAuth 自動跳轉",
"loginOauthAutoRedirectSubtitle": "You will be automatically redirected to your OAuth provider to authenticate.", "loginOauthAutoRedirectSubtitle": "自動跳轉到 OAuth 供應商進行身份驗證。",
"loginOauthAutoRedirectButton": "Redirect now", "loginOauthAutoRedirectButton": "立即重新導向",
"continueTitle": "繼續", "continueTitle": "繼續",
"continueRedirectingTitle": "重新導向中……", "continueRedirectingTitle": "重新導向中……",
"continueRedirectingSubtitle": "您即將被重新導向至應用程式", "continueRedirectingSubtitle": "您即將被重新導向至應用程式",
"continueRedirectManually": "Redirect me manually", "continueRedirectManually": "手動重新導向",
"continueInsecureRedirectTitle": "不安全的重新導向", "continueInsecureRedirectTitle": "不安全的重新導向",
"continueInsecureRedirectSubtitle": "您正嘗試從安全的 <code>https</code> 重新導向至不安全的 <code>http</code>。您確定要繼續嗎?", "continueInsecureRedirectSubtitle": "您正嘗試從安全的 <code>https</code> 重新導向至不安全的 <code>http</code>。您確定要繼續嗎?",
"continueUntrustedRedirectTitle": "Untrusted redirect", "continueUntrustedRedirectTitle": "不受信任的重新導向",
"continueUntrustedRedirectSubtitle": "You are trying to redirect to a domain that does not match your configured domain (<code>{{cookieDomain}}</code>). Are you sure you want to continue?", "continueUntrustedRedirectSubtitle": "你嘗試重新導向的域名與設定不符(<code>{{cookieDomain}}</code>)。你確定要繼續嗎?",
"logoutFailTitle": "登出失敗", "logoutFailTitle": "登出失敗",
"logoutFailSubtitle": "請再試一次", "logoutFailSubtitle": "請再試一次",
"logoutSuccessTitle": "登出成功", "logoutSuccessTitle": "登出成功",
@@ -52,11 +52,11 @@
"failedToFetchProvidersTitle": "載入驗證供應商失敗。請檢查您的設定。", "failedToFetchProvidersTitle": "載入驗證供應商失敗。請檢查您的設定。",
"errorTitle": "發生錯誤", "errorTitle": "發生錯誤",
"errorSubtitle": "執行此操作時發生錯誤。請檢查主控台以獲取更多資訊。", "errorSubtitle": "執行此操作時發生錯誤。請檢查主控台以獲取更多資訊。",
"forgotPasswordMessage": "You can reset your password by changing the `USERS` environment variable.", "forgotPasswordMessage": "透過修改 `USERS` 環境變數,你可以重設你的密碼。",
"fieldRequired": "This field is required", "fieldRequired": "此為必填欄位",
"invalidInput": "Invalid input", "invalidInput": "無效的輸入",
"domainWarningTitle": "Invalid Domain", "domainWarningTitle": "無效的網域",
"domainWarningSubtitle": "This instance is configured to be accessed from <code>{{appUrl}}</code>, but <code>{{currentUrl}}</code> is being used. If you proceed, you may encounter issues with authentication.", "domainWarningSubtitle": "此服務設定為透過 <code>{{appUrl}}</code> 存取,但目前使用的是 <code>{{currentUrl}}</code>。若繼續操作,可能會遇到驗證問題。",
"ignoreTitle": "Ignore", "ignoreTitle": "忽略",
"goToCorrectDomainTitle": "Go to correct domain" "goToCorrectDomainTitle": "前往正確域名"
} }

View File

@@ -147,6 +147,8 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) {
} }
if !controller.auth.IsEmailWhitelisted(user.Email) { if !controller.auth.IsEmailWhitelisted(user.Email) {
log.Warn().Str("email", user.Email).Msg("Email not whitelisted")
queries, err := query.Values(config.UnauthorizedQuery{ queries, err := query.Values(config.UnauthorizedQuery{
Username: user.Email, Username: user.Email,
}) })
@@ -181,14 +183,18 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) {
username = strings.Replace(user.Email, "@", "_", -1) username = strings.Replace(user.Email, "@", "_", -1)
} }
err = controller.auth.CreateSessionCookie(c, &config.SessionCookie{ sessionCookie := config.SessionCookie{
Username: username, Username: username,
Name: name, Name: name,
Email: user.Email, Email: user.Email,
Provider: req.Provider, Provider: req.Provider,
OAuthGroups: utils.CoalesceToString(user.Groups), OAuthGroups: utils.CoalesceToString(user.Groups),
OAuthName: service.GetName(), OAuthName: service.GetName(),
}) }
log.Trace().Interface("session_cookie", sessionCookie).Msg("Creating session cookie")
err = controller.auth.CreateSessionCookie(c, &sessionCookie)
if err != nil { if err != nil {
log.Error().Err(err).Msg("Failed to create session cookie") log.Error().Err(err).Msg("Failed to create session cookie")

View File

@@ -138,12 +138,16 @@ func (controller *UserController) loginHandler(c *gin.Context) {
} }
} }
err = controller.auth.CreateSessionCookie(c, &config.SessionCookie{ sessionCookie := config.SessionCookie{
Username: req.Username, Username: req.Username,
Name: utils.Capitalize(req.Username), Name: utils.Capitalize(req.Username),
Email: fmt.Sprintf("%s@%s", strings.ToLower(req.Username), controller.config.CookieDomain), Email: fmt.Sprintf("%s@%s", strings.ToLower(req.Username), controller.config.CookieDomain),
Provider: "username", Provider: "username",
}) }
log.Trace().Interface("session_cookie", sessionCookie).Msg("Creating session cookie")
err = controller.auth.CreateSessionCookie(c, &sessionCookie)
if err != nil { if err != nil {
log.Error().Err(err).Msg("Failed to create session cookie") log.Error().Err(err).Msg("Failed to create session cookie")
@@ -243,12 +247,16 @@ func (controller *UserController) totpHandler(c *gin.Context) {
controller.auth.RecordLoginAttempt(rateIdentifier, true) controller.auth.RecordLoginAttempt(rateIdentifier, true)
err = controller.auth.CreateSessionCookie(c, &config.SessionCookie{ sessionCookie := config.SessionCookie{
Username: user.Username, Username: user.Username,
Name: utils.Capitalize(user.Username), Name: utils.Capitalize(user.Username),
Email: fmt.Sprintf("%s@%s", strings.ToLower(user.Username), controller.config.CookieDomain), Email: fmt.Sprintf("%s@%s", strings.ToLower(user.Username), controller.config.CookieDomain),
Provider: "username", Provider: "username",
}) }
log.Trace().Interface("session_cookie", sessionCookie).Msg("Creating session cookie")
err = controller.auth.CreateSessionCookie(c, &sessionCookie)
if err != nil { if err != nil {
log.Error().Err(err).Msg("Failed to create session cookie") log.Error().Err(err).Msg("Failed to create session cookie")

View File

@@ -200,3 +200,23 @@ func GetOAuthProvidersConfig(env []string, args []string, appUrl string) (map[st
// Return combined providers // Return combined providers
return providers, nil return providers, nil
} }
func ShoudLogJSON(environ []string, args []string) bool {
for _, e := range environ {
pair := strings.SplitN(e, "=", 2)
if len(pair) == 2 && pair[0] == "LOG_JSON" && strings.ToLower(pair[1]) == "true" {
return true
}
}
for _, arg := range args[1:] {
if strings.HasPrefix(arg, "--log-json=") {
value := strings.SplitN(arg, "=", 2)[1]
if strings.ToLower(value) == "true" {
return true
}
}
}
return false
}

View File

@@ -278,3 +278,21 @@ func TestGetOAuthProvidersConfig(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, expected, result) assert.DeepEqual(t, expected, result)
} }
func TestShoudLogJSON(t *testing.T) {
// Test with no env or args
result := utils.ShoudLogJSON([]string{"FOO=bar"}, []string{"tinyauth", "--foo-bar=baz"})
assert.Equal(t, false, result)
// Test with env variable set
result = utils.ShoudLogJSON([]string{"LOG_JSON=true"}, []string{"tinyauth", "--foo-bar=baz"})
assert.Equal(t, true, result)
// Test with flag set
result = utils.ShoudLogJSON([]string{"FOO=bar"}, []string{"tinyauth", "--log-json=true"})
assert.Equal(t, true, result)
// Test with both env and flag set to false
result = utils.ShoudLogJSON([]string{"LOG_JSON=false"}, []string{"tinyauth", "--log-json=false"})
assert.Equal(t, false, result)
}

View File

@@ -4,12 +4,16 @@ import (
"os" "os"
"time" "time"
"tinyauth/cmd" "tinyauth/cmd"
"tinyauth/internal/utils"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
func main() { func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}).With().Timestamp().Caller().Logger() log.Logger = log.Logger.With().Timestamp().Caller().Logger()
if !utils.ShoudLogJSON(os.Environ(), os.Args) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339})
}
cmd.Run() cmd.Run()
} }