|
8 | 8 | "net/http"
|
9 | 9 | "time"
|
10 | 10 |
|
| 11 | + "github.com/lestrrat-go/jwx/jwt" |
11 | 12 | codeverifier "github.com/nirasan/go-oauth-pkce-code-verifier"
|
| 13 | + "github.com/sirupsen/logrus" |
12 | 14 | "golang.org/x/oauth2"
|
13 | 15 | )
|
14 | 16 |
|
@@ -73,8 +75,51 @@ func handleRedirectGoogle(state, redirectURI string, codeVerifier *codeverifier.
|
73 | 75 | return
|
74 | 76 | }
|
75 | 77 |
|
76 |
| - successfulResponse(w, "Successfully authenticated 👌 Close me pls", r.Header.Get("user-agent")) |
| 78 | + ret, err := consoleURL(ctx, exchangeResponse.IDToken, "connected") |
| 79 | + if err != nil { |
| 80 | + logrus.Println("Failed to get console URL: " + err.Error()) |
| 81 | + successfulResponse(w, "Successfully authenticated 👌 Close me pls", r.Header.Get("user-agent")) |
| 82 | + } else { |
| 83 | + http.Redirect(w, r, ret, http.StatusSeeOther) |
| 84 | + } |
| 85 | + |
77 | 86 | tokens := &Tokens{Token: exchangeResponse.Token, IDToken: exchangeResponse.IDToken}
|
78 | 87 | authFlowChan <- &authFlowResponse{Tokens: tokens, err: nil}
|
79 | 88 | }
|
80 | 89 | }
|
| 90 | + |
| 91 | +func consoleURL(ctx context.Context, idToken, state string) (string, error) { |
| 92 | + // Parse id token to get domain |
| 93 | + t, err := jwt.ParseString(idToken) |
| 94 | + if err != nil { |
| 95 | + return "", err |
| 96 | + } |
| 97 | + hd, _ := t.Get("hd") |
| 98 | + domain, _ := hd.(string) |
| 99 | + |
| 100 | + if domain == "" { |
| 101 | + return "", fmt.Errorf("could not find domain in id token") |
| 102 | + } |
| 103 | + |
| 104 | + url := fmt.Sprintf("https://storage.googleapis.com/nais-tenant-data/%s.json", domain) |
| 105 | + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) |
| 106 | + if err != nil { |
| 107 | + return "", err |
| 108 | + } |
| 109 | + |
| 110 | + resp, err := http.DefaultClient.Do(req) |
| 111 | + if err != nil { |
| 112 | + return "", err |
| 113 | + } |
| 114 | + defer resp.Body.Close() |
| 115 | + |
| 116 | + d := struct { |
| 117 | + ConsoleURL string `json:"consoleUrl"` |
| 118 | + }{} |
| 119 | + err = json.NewDecoder(resp.Body).Decode(&d) |
| 120 | + if err != nil { |
| 121 | + return "", err |
| 122 | + } |
| 123 | + |
| 124 | + return fmt.Sprintf("https://%s?naisdevice=%s", d.ConsoleURL, state), nil |
| 125 | +} |
0 commit comments