-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
42 lines (37 loc) · 1.58 KB
/
Copy pathauth.go
File metadata and controls
42 lines (37 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package core
import (
"errors"
"strings"
)
// KeySurface identifies the Sendmux API key category a client accepts.
type KeySurface string
const (
// KeySurfaceRoot accepts root/team API keys.
KeySurfaceRoot KeySurface = "root"
// KeySurfaceMailbox accepts mailbox-scoped API keys or scoped agent tokens.
KeySurfaceMailbox KeySurface = "mailbox"
// KeySurfaceSending accepts send-capable mailbox API keys or owner-approved agent tokens.
KeySurfaceSending KeySurface = "sending"
)
// ValidateAPIKey validates the key prefix for a Sendmux surface.
func ValidateAPIKey(apiKey string, surface KeySurface) error {
switch {
case apiKey == "":
return errors.New("sendmux: api key is required")
case strings.ContainsAny(apiKey, "\r\n"):
return errors.New("sendmux: api key must not contain control newlines")
case surface == KeySurfaceRoot && !strings.HasPrefix(apiKey, "smx_root_"):
return errors.New("sendmux: root API key must start with smx_root_")
case surface == KeySurfaceMailbox && !isMailboxCompatibleAPIKey(apiKey):
return errors.New("sendmux: mailbox API key must start with smx_mbx_ or smx_agent_")
case surface == KeySurfaceSending && !isMailboxCompatibleAPIKey(apiKey):
return errors.New("sendmux: sending API key must start with smx_mbx_ or smx_agent_")
case surface != KeySurfaceRoot && surface != KeySurfaceMailbox && surface != KeySurfaceSending:
return errors.New("sendmux: unknown API key surface")
default:
return nil
}
}
func isMailboxCompatibleAPIKey(apiKey string) bool {
return strings.HasPrefix(apiKey, "smx_mbx_") || strings.HasPrefix(apiKey, "smx_agent_")
}