2025-01-05 04:56:57 +00:00
|
|
|
// Copyright 2025, Command Line Inc.
|
2024-08-21 22:04:39 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
|
|
package authkey
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var authkey string
|
|
|
|
|
|
2024-10-27 20:12:41 +00:00
|
|
|
const WaveAuthKeyEnv = "WAVETERM_AUTH_KEY"
|
2024-08-26 20:55:47 +00:00
|
|
|
const AuthKeyHeader = "X-AuthKey"
|
|
|
|
|
|
|
|
|
|
func ValidateIncomingRequest(r *http.Request) error {
|
|
|
|
|
reqAuthKey := r.Header.Get(AuthKeyHeader)
|
|
|
|
|
if reqAuthKey == "" {
|
|
|
|
|
return fmt.Errorf("no x-authkey header")
|
|
|
|
|
}
|
|
|
|
|
if reqAuthKey != GetAuthKey() {
|
|
|
|
|
return fmt.Errorf("x-authkey header is invalid")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-08-21 22:04:39 +00:00
|
|
|
|
|
|
|
|
func SetAuthKeyFromEnv() error {
|
2024-10-27 20:12:41 +00:00
|
|
|
authkey = os.Getenv(WaveAuthKeyEnv)
|
2024-08-21 22:04:39 +00:00
|
|
|
if authkey == "" {
|
|
|
|
|
return fmt.Errorf("no auth key found in environment variables")
|
|
|
|
|
}
|
2024-10-27 20:12:41 +00:00
|
|
|
os.Unsetenv(WaveAuthKeyEnv)
|
2024-08-21 22:04:39 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetAuthKey() string {
|
|
|
|
|
return authkey
|
|
|
|
|
}
|