123 lines
3.0 KiB
Go
123 lines
3.0 KiB
Go
package auth
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// AuthStep 授权步骤
|
|
type AuthStep string
|
|
|
|
const (
|
|
StepBrowserStart AuthStep = "browser_start" // 启动浏览器
|
|
StepNavigate AuthStep = "navigate" // 访问授权页面
|
|
StepInputEmail AuthStep = "input_email" // 输入邮箱
|
|
StepSubmitEmail AuthStep = "submit_email" // 提交邮箱
|
|
StepInputPassword AuthStep = "input_password" // 输入密码
|
|
StepSubmitPassword AuthStep = "submit_password" // 提交密码
|
|
StepSelectWorkspace AuthStep = "select_workspace" // 选择工作区
|
|
StepConsent AuthStep = "consent" // 授权同意
|
|
StepWaitCallback AuthStep = "wait_callback" // 等待回调
|
|
StepExtractCode AuthStep = "extract_code" // 提取授权码
|
|
StepComplete AuthStep = "complete" // 完成授权
|
|
)
|
|
|
|
// AuthLogEntry 授权日志条目
|
|
type AuthLogEntry struct {
|
|
Step AuthStep
|
|
Message string
|
|
Duration time.Duration
|
|
Timestamp time.Time
|
|
IsError bool
|
|
}
|
|
|
|
// AuthLogger 授权日志记录器
|
|
type AuthLogger struct {
|
|
email string
|
|
teamPrefix string
|
|
memberIdx int
|
|
callback func(entry AuthLogEntry)
|
|
startTime time.Time
|
|
stepStart time.Time
|
|
mu sync.Mutex
|
|
}
|
|
|
|
// NewAuthLogger 创建授权日志记录器
|
|
func NewAuthLogger(email, teamPrefix string, memberIdx int, callback func(entry AuthLogEntry)) *AuthLogger {
|
|
return &AuthLogger{
|
|
email: email,
|
|
teamPrefix: teamPrefix,
|
|
memberIdx: memberIdx,
|
|
callback: callback,
|
|
startTime: time.Now(),
|
|
stepStart: time.Now(),
|
|
}
|
|
}
|
|
|
|
// LogStep 记录步骤
|
|
func (l *AuthLogger) LogStep(step AuthStep, format string, args ...interface{}) {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
|
|
now := time.Now()
|
|
entry := AuthLogEntry{
|
|
Step: step,
|
|
Message: fmt.Sprintf(format, args...),
|
|
Duration: now.Sub(l.stepStart),
|
|
Timestamp: now,
|
|
IsError: false,
|
|
}
|
|
l.stepStart = now
|
|
|
|
if l.callback != nil {
|
|
l.callback(entry)
|
|
}
|
|
}
|
|
|
|
// LogError 记录错误
|
|
func (l *AuthLogger) LogError(step AuthStep, format string, args ...interface{}) {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
|
|
now := time.Now()
|
|
entry := AuthLogEntry{
|
|
Step: step,
|
|
Message: fmt.Sprintf(format, args...),
|
|
Duration: now.Sub(l.stepStart),
|
|
Timestamp: now,
|
|
IsError: true,
|
|
}
|
|
l.stepStart = now
|
|
|
|
if l.callback != nil {
|
|
l.callback(entry)
|
|
}
|
|
}
|
|
|
|
// TotalDuration 获取总耗时
|
|
func (l *AuthLogger) TotalDuration() time.Duration {
|
|
return time.Since(l.startTime)
|
|
}
|
|
|
|
// StepName 获取步骤中文名称
|
|
func StepName(step AuthStep) string {
|
|
names := map[AuthStep]string{
|
|
StepBrowserStart: "启动浏览器",
|
|
StepNavigate: "访问授权页",
|
|
StepInputEmail: "输入邮箱",
|
|
StepSubmitEmail: "提交邮箱",
|
|
StepInputPassword: "输入密码",
|
|
StepSubmitPassword: "提交密码",
|
|
StepSelectWorkspace: "选择工作区",
|
|
StepConsent: "授权同意",
|
|
StepWaitCallback: "等待回调",
|
|
StepExtractCode: "提取授权码",
|
|
StepComplete: "完成授权",
|
|
}
|
|
if name, ok := names[step]; ok {
|
|
return name
|
|
}
|
|
return string(step)
|
|
}
|