2024-08-22 00:04:39 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package authkey
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var authkey string
|
|
|
|
|
2024-10-27 21:12:41 +01:00
|
|
|
const WaveAuthKeyEnv = "WAVETERM_AUTH_KEY"
|
2024-08-26 22:55:47 +02: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-22 00:04:39 +02:00
|
|
|
|
|
|
|
func SetAuthKeyFromEnv() error {
|
2024-10-27 21:12:41 +01:00
|
|
|
authkey = os.Getenv(WaveAuthKeyEnv)
|
2024-08-22 00:04:39 +02:00
|
|
|
if authkey == "" {
|
|
|
|
return fmt.Errorf("no auth key found in environment variables")
|
|
|
|
}
|
2024-10-27 21:12:41 +01:00
|
|
|
os.Unsetenv(WaveAuthKeyEnv)
|
2024-08-22 00:04:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAuthKey() string {
|
|
|
|
return authkey
|
|
|
|
}
|