-
Notifications
You must be signed in to change notification settings - Fork 415
/
hosted_domain.go
77 lines (66 loc) · 2.14 KB
/
hosted_domain.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
/*
* Copyright 2018- The Pixie 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package controllers
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const (
googleIdentityProvider = "google-oauth2"
)
func init() {
pflag.String("google_oauth_userinfo_url", "https://www.googleapis.com/oauth2/v2/userinfo", "Google OAuth2 URL for userinfo.")
}
// retrieveGoogleHostedDomain returns a DomainName for a Google OAuth access token.
// This is set for users that are part of the Google Cloud org domain.
// It should be blank for GMail users.
func retrieveGoogleHostedDomain(accessToken string) (string, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", viper.GetString("google_oauth_userinfo_url"), nil)
if err != nil {
return "", err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", errors.New("bad response from google")
}
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return "", err
}
var userInfo struct {
// HostedDomain is the hosted G Suite domain of the user. Provided only if the user belongs to a hosted domain.
// see https://developers.google.com/identity/protocols/oauth2/openid-connect#an-id-tokens-payload for more info.
HostedDomain string `json:"hd,omitempty"`
}
if err = json.Unmarshal(body, &userInfo); err != nil {
return "", err
}
return userInfo.HostedDomain, nil
}