From 77a6b91be8791003170c27b1d4fc49031c8ca284 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Tue, 23 Jun 2020 15:00:58 +0900 Subject: [PATCH] Change authentication success page more descriptive (#312) * Refactor: rename to authcode_browser.go * Change authentication success page more descriptive --- pkg/adaptors/oidcclient/oidcclient.go | 2 ++ pkg/templates/authcode_browser.go | 35 +++++++++++++++++++ pkg/templates/httpserver/httpserver.go | 17 +++++++++ pkg/templates/package.go | 6 ++++ .../{authcode.go => authcode_browser.go} | 2 ++ ...hcode_test.go => authcode_browser_test.go} | 0 6 files changed, 62 insertions(+) create mode 100644 pkg/templates/authcode_browser.go create mode 100644 pkg/templates/httpserver/httpserver.go create mode 100644 pkg/templates/package.go rename pkg/usecases/authentication/{authcode.go => authcode_browser.go} (95%) rename pkg/usecases/authentication/{authcode_test.go => authcode_browser_test.go} (100%) diff --git a/pkg/adaptors/oidcclient/oidcclient.go b/pkg/adaptors/oidcclient/oidcclient.go index f2f3d06b..0c44cfdf 100644 --- a/pkg/adaptors/oidcclient/oidcclient.go +++ b/pkg/adaptors/oidcclient/oidcclient.go @@ -48,6 +48,7 @@ type GetTokenByAuthCodeInput struct { PKCEParams pkce.Params RedirectURLHostname string AuthRequestExtraParams map[string]string + LocalServerSuccessHTML string } // TokenSet represents an output DTO of @@ -85,6 +86,7 @@ func (c *client) GetTokenByAuthCode(ctx context.Context, in GetTokenByAuthCodeIn LocalServerBindAddress: in.BindAddress, LocalServerReadyChan: localServerReadyChan, RedirectURLHostname: in.RedirectURLHostname, + LocalServerSuccessHTML: in.LocalServerSuccessHTML, } token, err := oauth2cli.GetToken(ctx, config) if err != nil { diff --git a/pkg/templates/authcode_browser.go b/pkg/templates/authcode_browser.go new file mode 100644 index 00000000..68ab5da6 --- /dev/null +++ b/pkg/templates/authcode_browser.go @@ -0,0 +1,35 @@ +package templates + +// AuthCodeBrowserSuccessHTML is the success page on browser based authentication. +const AuthCodeBrowserSuccessHTML = ` + + + + + Authenticated + + + + +
+

Authenticated

+

You have logged in to the cluster. You can close this window.

+
+ + +` diff --git a/pkg/templates/httpserver/httpserver.go b/pkg/templates/httpserver/httpserver.go new file mode 100644 index 00000000..746fc7f6 --- /dev/null +++ b/pkg/templates/httpserver/httpserver.go @@ -0,0 +1,17 @@ +package main + +import ( + "log" + "net/http" + + "github.com/int128/kubelogin/pkg/templates" +) + +func main() { + http.HandleFunc("/AuthCodeBrowserSuccessHTML", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("content-type", "text/html") + _, _ = w.Write([]byte(templates.AuthCodeBrowserSuccessHTML)) + }) + log.Printf("http://localhost:8000/AuthCodeBrowserSuccessHTML") + log.Fatal(http.ListenAndServe("127.0.0.1:8000", nil)) +} diff --git a/pkg/templates/package.go b/pkg/templates/package.go new file mode 100644 index 00000000..f27f93d2 --- /dev/null +++ b/pkg/templates/package.go @@ -0,0 +1,6 @@ +// Package templates provides templates such as HTML and messages. +// +// You can preview HTML pages by running httpserver package. +// go run ./httpserver +// +package templates diff --git a/pkg/usecases/authentication/authcode.go b/pkg/usecases/authentication/authcode_browser.go similarity index 95% rename from pkg/usecases/authentication/authcode.go rename to pkg/usecases/authentication/authcode_browser.go index cfcfc178..e8903b0f 100644 --- a/pkg/usecases/authentication/authcode.go +++ b/pkg/usecases/authentication/authcode_browser.go @@ -8,6 +8,7 @@ import ( "github.com/int128/kubelogin/pkg/adaptors/oidcclient" "github.com/int128/kubelogin/pkg/domain/oidc" "github.com/int128/kubelogin/pkg/domain/pkce" + "github.com/int128/kubelogin/pkg/templates" "golang.org/x/sync/errgroup" "golang.org/x/xerrors" ) @@ -39,6 +40,7 @@ func (u *AuthCode) Do(ctx context.Context, o *AuthCodeOption, client oidcclient. PKCEParams: p, RedirectURLHostname: o.RedirectURLHostname, AuthRequestExtraParams: o.AuthRequestExtraParams, + LocalServerSuccessHTML: templates.AuthCodeBrowserSuccessHTML, } readyChan := make(chan string, 1) defer close(readyChan) diff --git a/pkg/usecases/authentication/authcode_test.go b/pkg/usecases/authentication/authcode_browser_test.go similarity index 100% rename from pkg/usecases/authentication/authcode_test.go rename to pkg/usecases/authentication/authcode_browser_test.go