forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticator.go
146 lines (120 loc) · 4.02 KB
/
authenticator.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ca
import (
"fmt"
"strings"
oidc "github.com/coreos/go-oidc"
"golang.org/x/net/context"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"istio.io/istio/security/pkg/pki/util"
)
const (
bearerTokenPrefix = "Bearer "
httpAuthHeader = "authorization"
idTokenIssuer = "https://accounts.google.com"
)
// authSource represents where authentication result is derived from.
type authSource int
const (
authSourceClientCertificate authSource = iota
authSourceIDToken
)
type caller struct {
authSource authSource
identities []string
}
type authenticator interface {
authenticate(ctx context.Context) (*caller, error)
}
// An authenticator that extracts identities from client certificate.
type clientCertAuthenticator struct{}
// authenticate extracts identities from presented client certificates. This
// method assumes that certificate chain has been properly validated before
// this method is called. In other words, this method does not do certificate
// chain validation itself.
func (cca *clientCertAuthenticator) authenticate(ctx context.Context) (*caller, error) {
peer, ok := peer.FromContext(ctx)
if !ok {
return nil, fmt.Errorf("no client certificate is presented")
}
if authType := peer.AuthInfo.AuthType(); authType != "tls" {
return nil, fmt.Errorf("unsupported auth type: %q", authType)
}
tlsInfo := peer.AuthInfo.(credentials.TLSInfo)
chains := tlsInfo.State.VerifiedChains
if len(chains) == 0 || len(chains[0]) == 0 {
return nil, fmt.Errorf("no verified chain is found")
}
ids, err := util.ExtractIDs(chains[0][0].Extensions)
if err != nil {
return nil, err
}
return &caller{
authSource: authSourceClientCertificate,
identities: ids,
}, nil
}
// An authenticator that extracts identity from JWT. The JWT is required to be
// transmitted using the "Bearer" authentication scheme.
type idTokenAuthenticator struct {
verifier *oidc.IDTokenVerifier
}
func newIDTokenAuthenticator(aud string) (*idTokenAuthenticator, error) {
provider, err := oidc.NewProvider(context.Background(), idTokenIssuer)
if err != nil {
return nil, err
}
verifier := provider.Verifier(&oidc.Config{ClientID: aud})
return &idTokenAuthenticator{verifier}, nil
}
func (ja *idTokenAuthenticator) authenticate(ctx context.Context) (*caller, error) {
bearerToken, err := extractBearerToken(ctx)
if err != nil {
return nil, fmt.Errorf("ID token extraction error: %v", err)
}
idToken, err := ja.verifier.Verify(context.Background(), bearerToken)
if err != nil {
return nil, fmt.Errorf("failed to verify the ID token (error %v)", err)
}
// for GCP-issued JWT, the service account is in the "email" field
var sa struct {
Email string `json:"email"`
}
if err := idToken.Claims(&sa); err != nil {
return nil, fmt.Errorf("failed to extract email field from ID token: %v", err)
}
return &caller{
authSource: authSourceIDToken,
identities: []string{sa.Email},
}, nil
}
func extractBearerToken(ctx context.Context) (string, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return "", fmt.Errorf("no metadata is attached")
}
authHeader, exists := md[httpAuthHeader]
if !exists {
return "", fmt.Errorf("no HTTP authorization header exists")
}
for _, value := range authHeader {
if strings.HasPrefix(value, bearerTokenPrefix) {
return strings.TrimPrefix(value, bearerTokenPrefix), nil
}
}
return "", fmt.Errorf("no bearer token exists in HTTP authorization header")
}