@@ -34,8 +34,12 @@ const (
34
34
// IAPFields contains the values for the headers retrieved from Identity Aware
35
35
// Proxy.
36
36
type IAPFields struct {
37
+ // Email contains the user's email address
38
+ // For example, "accounts.google.com:[email protected] "
37
39
Email string
38
- ID string
40
+ // ID contains a unique identifier for the user
41
+ // For example, "accounts.google.com:userIDvalue"
42
+ ID string
39
43
}
40
44
41
45
// IAPFromContext retrieves the IAPFields stored in the context if it exists.
@@ -96,7 +100,12 @@ func contextWithIAPMD(ctx context.Context, md metadata.MD) (context.Context, err
96
100
if iap .ID , err = retrieveFn (md , iapHeaderID ); err != nil {
97
101
return ctx , fmt .Errorf ("unable to retrieve metadata field: %s" , iapHeaderID )
98
102
}
99
- return context .WithValue (ctx , contextIAP , iap ), nil
103
+ return ContextWithIAP (ctx , iap ), nil
104
+ }
105
+
106
+ // ContextWithIAP adds the iap fields to the context.
107
+ func ContextWithIAP (ctx context.Context , iap IAPFields ) context.Context {
108
+ return context .WithValue (ctx , contextIAP , iap )
100
109
}
101
110
102
111
// RequireIAPAuthUnaryInterceptor creates an authentication interceptor for a GRPC
@@ -132,3 +141,28 @@ func IAPAudienceGCE(projectNumber int64, serviceID string) string {
132
141
func IAPAudienceAppEngine (projectNumber int64 , projectID string ) string {
133
142
return fmt .Sprintf ("/projects/%d/apps/%s" , projectNumber , projectID )
134
143
}
144
+
145
+ // FakeContextWithOutgoingIAPAuth adds the iap fields to the metadata of an outgoing GRPC request and
146
+ // should only be used for testing.
147
+ func FakeContextWithOutgoingIAPAuth (ctx context.Context , iap IAPFields ) context.Context {
148
+ md := metadata .New (map [string ]string {
149
+ iapHeaderEmail : iap .Email ,
150
+ iapHeaderID : iap .ID ,
151
+ iapHeaderJWT : "test-jwt" ,
152
+ })
153
+ return metadata .NewOutgoingContext (ctx , md )
154
+ }
155
+
156
+ // FakeIAPAuthFunc provides a fake IAP authentication validation and should only be used for testing.
157
+ func FakeIAPAuthFunc () grpcauth.AuthFunc {
158
+ return iapAuthFunc ("TESTING" , func (ctx context.Context , token , audiance string ) (* idtoken.Payload , error ) { return nil , nil })
159
+ }
160
+
161
+ // FakeIAPAuthInterceptorOptions provides the GRPC server options for fake IAP authentication
162
+ // and should only be used for testing.
163
+ func FakeIAPAuthInterceptorOptions () []grpc.ServerOption {
164
+ return []grpc.ServerOption {
165
+ grpc .UnaryInterceptor (grpcauth .UnaryServerInterceptor (FakeIAPAuthFunc ())),
166
+ grpc .StreamInterceptor (grpcauth .StreamServerInterceptor (FakeIAPAuthFunc ())),
167
+ }
168
+ }
0 commit comments