forked from google/keytransparency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.go
122 lines (108 loc) · 3.37 KB
/
google.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
// Copyright 2016 Google Inc. All Rights Reserved.
//
// 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 authentication
import (
"context"
"log"
"net/http"
"strings"
"golang.org/x/oauth2"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
gAPI "google.golang.org/api/oauth2/v2"
)
// GRPC stores authentication information in the "authorization" header.
var (
// E2EScope authorizes a user to change their keys in the keyserver.
E2EScope = "https://www.googleapis.com/auth/e2ekeys"
// RequiredScopes is the set of scopes the server requires for a user to change keys.
RequiredScopes = []string{gAPI.UserinfoEmailScope, E2EScope}
)
// GAuth authenticates Google users through the Google TokenInfo API endpoint.
type GAuth struct {
service *gAPI.Service
}
// NewGoogleAuth creates a new authenticator for Google users.
func NewGoogleAuth() (*GAuth, error) {
googleService, err := gAPI.New(http.DefaultClient)
if err != nil {
return nil, err
}
return &GAuth{service: googleService}, nil
}
// AuthFunc authenticate the information present in ctx.
func (a *GAuth) AuthFunc(ctx context.Context) (context.Context, error) {
token, err := parseToken(ctx)
if err != nil {
return nil, err
}
tokenInfo, err := a.validateToken(token)
if err != nil {
return nil, err
}
if !tokenInfo.VerifiedEmail {
return nil, status.Error(codes.Unauthenticated, "auth: unverified email address")
}
// Validate scopes.
scopes := strings.Split(tokenInfo.Scope, " ")
diff := setDifference(RequiredScopes, scopes)
if len(diff) > 0 {
log.Printf("Failed auth: missing scopes %v", diff)
return nil, status.Error(codes.Unauthenticated, "auth: missing scope")
}
return context.WithValue(ctx, securityContextKey, &SecurityContext{
Email: tokenInfo.Email,
}), nil
}
// setDifference returns all the elements of A that are not elements of B.
func setDifference(a, b []string) []string {
// Build set b.
setB := make(map[string]bool)
for _, e := range b {
setB[e] = true
}
// Iterate through A, adding elements that are not in B.
var diff []string
for _, e := range a {
if _, ok := setB[e]; !ok {
diff = append(diff, e)
}
}
return diff
}
func parseToken(ctx context.Context) (*oauth2.Token, error) {
accessToken, err := grpc_auth.AuthFromMD(ctx, "bearer")
if err != nil {
return nil, err
}
return &oauth2.Token{
TokenType: "bearer",
AccessToken: accessToken,
}, nil
}
// validateToken makes an https request to the tokeninfo API using the access
// token provided in the header.
func (a *GAuth) validateToken(token *oauth2.Token) (*gAPI.Tokeninfo, error) {
if !token.Valid() {
return nil, status.Error(codes.Unauthenticated, "auth: invalid token")
}
infoCall := a.service.Tokeninfo()
infoCall.AccessToken(token.AccessToken)
info, err := infoCall.Do()
if err != nil {
return nil, err
}
return info, nil
}