-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
98 lines (80 loc) · 2.38 KB
/
auth.go
File metadata and controls
98 lines (80 loc) · 2.38 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package ipfs
import (
"context"
"net/http"
"time"
"github.com/golang-jwt/jwt/v5"
)
// AuthProvider defines the interface for authentication providers.
// Implementations can add authentication headers and tokens to HTTP requests.
type AuthProvider interface {
// AddHeaders adds authentication headers to the HTTP request.
// This is called for each outbound request to apply authentication.
AddHeaders(ctx context.Context, req *http.Request) error
// Token returns the current authentication token.
// Returns an empty string if no token is set.
Token() string
// SetToken sets the authentication token.
SetToken(token string)
}
// JWTProvider is a JWT-based authentication provider.
// It automatically adds "Bearer {token}" authorization headers to requests.
type JWTProvider struct {
token string
}
// NewJWTProvider creates a new JWT authentication provider.
// The token parameter is the JWT token string.
func NewJWTProvider(token string) *JWTProvider {
return &JWTProvider{token: token}
}
// AddHeaders adds the authorization header with the JWT token.
// If no token is set, no header is added.
func (p *JWTProvider) AddHeaders(_ context.Context, req *http.Request) error {
if p.token != "" {
req.Header.Set("Authorization", "Bearer "+p.token)
}
return nil
}
// Token returns the current JWT token.
func (p *JWTProvider) Token() string {
return p.token
}
// SetToken sets a new JWT token.
// This can be used to refresh tokens during runtime.
func (p *JWTProvider) SetToken(token string) {
p.token = token
}
// Claims retrieves the JWT claims from the current token.
// Returns an error if the token is not valid or cannot be parsed.
func (p *JWTProvider) Claims() (jwt.MapClaims, error) {
if p.token == "" {
return nil, nil
}
parser := jwt.NewParser()
claims := jwt.MapClaims{}
// Parse without verification for claim inspection
_, _, err := parser.ParseUnverified(p.token, &claims)
if err != nil {
return nil, err
}
return claims, nil
}
// Valid checks if the current token is valid.
// This performs basic validation without full cryptographic verification.
func (p *JWTProvider) Valid() bool {
if p.token == "" {
return false
}
claims, err := p.Claims()
if err != nil {
return false
}
// Check if expired
if exp, ok := claims["exp"].(float64); ok {
currentTime := float64(time.Now().Unix())
if exp < currentTime {
return false
}
}
return true
}