Skip to content

Commit 18192bf

Browse files
authored
Redirect user to Console on successfull login (#405)
* Redirect user to Console on successfull login It will fall back to kek when we cannot decide on correct url * Upgrade to latest Go 1.23 to fix vulnerabilities
1 parent 9e1b7c1 commit 18192bf

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

.tool-versions

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
golang 1.23.3
1+
golang 1.23.6
22
protoc 23.4
33
protoc-gen-go 1.31.0
44
protoc-gen-go-grpc 1.3.0

internal/device-agent/auth/azure.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,8 @@ func handleRedirectAzure(state string, conf oauth2.Config, codeVerifier *codever
6363
return
6464
}
6565

66-
msg := `Successfully authenticated 👌 Close me pls
67-
<p style="text-align: center"><a href="https://console.nav.cloud.nais.io">Go to NAIS Console</a></p>
68-
`
69-
successfulResponse(w, msg, r.Header.Get("user-agent"))
66+
http.Redirect(w, r, "https://console.nav.cloud.nais.io/?naisdevice=1", http.StatusSeeOther)
67+
7068
authFlowChan <- &authFlowResponse{Tokens: &Tokens{Token: t}, err: nil}
7169
}
7270
}

internal/device-agent/auth/google.go

+46-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"net/http"
99
"time"
1010

11+
"github.com/lestrrat-go/jwx/jwt"
1112
codeverifier "github.com/nirasan/go-oauth-pkce-code-verifier"
13+
"github.com/sirupsen/logrus"
1214
"golang.org/x/oauth2"
1315
)
1416

@@ -73,8 +75,51 @@ func handleRedirectGoogle(state, redirectURI string, codeVerifier *codeverifier.
7375
return
7476
}
7577

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+
7786
tokens := &Tokens{Token: exchangeResponse.Token, IDToken: exchangeResponse.IDToken}
7887
authFlowChan <- &authFlowResponse{Tokens: tokens, err: nil}
7988
}
8089
}
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

Comments
 (0)