-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh_auth_server.go
More file actions
84 lines (69 loc) · 2.72 KB
/
Copy pathmesh_auth_server.go
File metadata and controls
84 lines (69 loc) · 2.72 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
package aegis
import (
"context"
"encoding/json"
"github.com/zoobz-io/sctx"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// meshAuthService implements the MeshAuthServer gRPC interface.
type meshAuthService struct {
UnimplementedMeshAuthServer
admin sctx.Admin[Metadata]
}
// NewMeshAuthService creates a new MeshAuth service backed by the given Admin.
func NewMeshAuthService(admin sctx.Admin[Metadata]) MeshAuthServer {
return &meshAuthService{admin: admin}
}
// ExchangeToken verifies a signed assertion and returns a service token.
func (s *meshAuthService) ExchangeToken(ctx context.Context, req *TokenExchangeRequest) (*TokenExchangeResponse, error) {
caller, err := CallerFromContext(ctx)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "failed to extract caller identity: %v", err)
}
var assertion sctx.SignedAssertion
if unmarshalErr := json.Unmarshal(req.Assertion, &assertion); unmarshalErr != nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to decode assertion: %v", unmarshalErr)
}
token, err := s.admin.Generate(ctx, caller.Certificate, assertion)
if err != nil {
return nil, status.Errorf(codes.PermissionDenied, "token exchange failed: %v", err)
}
fingerprint := sctx.GetFingerprint(caller.Certificate)
sc, ok := s.admin.GetContext(ctx, fingerprint)
if !ok {
return nil, status.Errorf(codes.Internal, "context not found after generation")
}
return &TokenExchangeResponse{
Token: string(token),
ExpiresAt: sc.ExpiresAt.Unix(),
}, nil
}
// RevokeToken revokes a token by certificate fingerprint.
// Only self-revocation is permitted — the caller's certificate fingerprint
// must match the requested fingerprint.
func (s *meshAuthService) RevokeToken(ctx context.Context, req *RevokeTokenRequest) (*RevokeTokenResponse, error) {
if req.Fingerprint == "" {
return nil, status.Error(codes.InvalidArgument, "fingerprint is required")
}
caller, err := CallerFromContext(ctx)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "failed to extract caller identity: %v", err)
}
callerFingerprint := sctx.GetFingerprint(caller.Certificate)
if req.Fingerprint != callerFingerprint {
return nil, status.Error(codes.PermissionDenied, "can only revoke own token")
}
if err := s.admin.RevokeByFingerprint(ctx, req.Fingerprint); err != nil {
return nil, status.Errorf(codes.Internal, "failed to revoke token: %v", err)
}
return &RevokeTokenResponse{}, nil
}
// MeshAuthRegistrar returns a ServiceRegistrar that registers the MeshAuth service.
func MeshAuthRegistrar(admin sctx.Admin[Metadata]) ServiceRegistrar {
server := NewMeshAuthService(admin)
return func(s *grpc.Server) {
RegisterMeshAuthServer(s, server)
}
}