Browse Source

1. 增加是否开启app权限校验的配置

main
lin_hl 1 week ago
parent
commit
76ef615a29
  1. 1
      config.yml
  2. 1
      config/config.go
  3. 30
      main.go

1
config.yml

@ -11,6 +11,7 @@ server:
# 认证密钥配置 # 认证密钥配置
auth: auth:
enabled: true
app_access_secret: "D4tBb9Y0oHSXRAyHLHpdKfXAuNCyCZ45AZxKJOhMJMs=" app_access_secret: "D4tBb9Y0oHSXRAyHLHpdKfXAuNCyCZ45AZxKJOhMJMs="
device_relay_secret: "p+JtJ8aHlM1lDYu7UGFanX8ALVt1pM1BQmKTpqTJccs=" device_relay_secret: "p+JtJ8aHlM1lDYu7UGFanX8ALVt1pM1BQmKTpqTJccs="

1
config/config.go

@ -20,6 +20,7 @@ type ServerConfig struct {
InstanceID string `mapstructure:"instance_id"` InstanceID string `mapstructure:"instance_id"`
} }
type AuthConfig struct { type AuthConfig struct {
Enabled bool `mapstructure:"enabled"`
AppAccessSecret string `mapstructure:"app_access_secret"` AppAccessSecret string `mapstructure:"app_access_secret"`
DeviceRelaySecret string `mapstructure:"device_relay_secret"` DeviceRelaySecret string `mapstructure:"device_relay_secret"`
} }

30
main.go

@ -245,14 +245,12 @@ func handleAppRequest(w http.ResponseWriter, r *http.Request) {
deviceSN := pathParts[1] deviceSN := pathParts[1]
// --- [App 认证逻辑 - 暂时注释,需要时取消注释即可] --- // --- [App 认证逻辑 - 暂时注释,需要时取消注释即可] ---
/* appUserID, err := authenticateAppRequest(r)
appUserID, err := authenticateAppRequest(r) if err != nil {
if err != nil { log.Printf("App authentication failed for device %s: %v", deviceSN, err)
log.Printf("App authentication failed for device %s: %v", deviceSN, err) http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "Unauthorized", http.StatusUnauthorized) return
return }
}
*/
sessionMutex.RLock() sessionMutex.RLock()
sessionInfo, ok := deviceSessions[deviceSN] sessionInfo, ok := deviceSessions[deviceSN]
@ -263,13 +261,11 @@ func handleAppRequest(w http.ResponseWriter, r *http.Request) {
return return
} }
/* --- [所有权检查 - 暂时注释] --- if config.Cfg.Auth.Enabled && sessionInfo.UserID != appUserID {
if sessionInfo.UserID != appUserID { log.Printf("Forb idden: App user '%s' attempted to access device '%s' owned by '%s'", appUserID, deviceSN, sessionInfo.UserID)
log.Printf("Forbidden: App user '%s' attempted to access device '%s' owned by '%s'", appUserID, deviceSN, sessionInfo.UserID)
http.Error(w, "Forbidden: you do not own this device", http.StatusForbidden) http.Error(w, "Forbidden: you do not own this device", http.StatusForbidden)
return return
} }
*/
proxy := &httputil.ReverseProxy{ proxy := &httputil.ReverseProxy{
Director: func(req *http.Request) { Director: func(req *http.Request) {
@ -310,14 +306,18 @@ func handleAppRequest(w http.ResponseWriter, r *http.Request) {
// authenticateAppRequest 和 verifyAppToken 保持不变,备用 // authenticateAppRequest 和 verifyAppToken 保持不变,备用
func authenticateAppRequest(r *http.Request) (string, error) { func authenticateAppRequest(r *http.Request) (string, error) {
if !config.Cfg.Auth.Enabled {
return "", nil
}
authHeader := r.Header.Get("Authorization") authHeader := r.Header.Get("Authorization")
if authHeader == "" { if authHeader == "" {
return "", errors.New("missing Authorization header") return "", errors.New("missing Authorization header")
} }
tokenString := strings.TrimPrefix(authHeader, "Bearer ") tokenString := strings.TrimPrefix(authHeader, "Bearer ")
if tokenString == authHeader { //if tokenString == authHeader {
return "", errors.New("authorization header format must be Bearer {token}") // return "", errors.New("authorization header format must be Bearer {token}")
} //}
claims, err := verifyAppToken(tokenString) claims, err := verifyAppToken(tokenString)
if err != nil { if err != nil {
return "", fmt.Errorf("app token verification failed: %w", err) return "", fmt.Errorf("app token verification failed: %w", err)

Loading…
Cancel
Save