From ab264487847b5ef24a466b2ab7b2e7d88b9d5016 Mon Sep 17 00:00:00 2001 From: azuki774s Date: Sat, 26 Oct 2024 22:12:00 +0900 Subject: [PATCH] ser redirect_url to github oauth2 --- internal/server/server.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/internal/server/server.go b/internal/server/server.go index 5f4c916..435770a 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "net/url" "os" "os/signal" "syscall" @@ -14,6 +13,9 @@ import ( "go.uber.org/zap" ) +const XCallBackHeader = "X-Callback-URL" +const githubOAuthauthorizeURL = "https://github.com/login/oauth/authorize" + type Server struct { Port int Authenticator Authenticator @@ -29,11 +31,6 @@ type Authenticator interface { HandlingGitHubOAuth(ctx context.Context, code string) (ok bool, err error) } -// https://hoge.example.com/callback/github -> https://hoge.example.com/ -func (s Server) getServerBaseURL(r *url.URL) string { - return r.Scheme + "://" + r.Host + s.BasePath -} - func (s Server) addHandler(r *chi.Mux) { r.Get("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("OK")) @@ -73,9 +70,18 @@ func (s Server) addHandler(r *chi.Mux) { }) r.Get("/login_page", func(w http.ResponseWriter, r *http.Request) { - clientId := os.Getenv("GITHUB_CLIENT_ID") // TODO - url := fmt.Sprintf("https://github.com/login/oauth/authorize?client_id=%s&scope=user:read", clientId) + clientId := os.Getenv("GITHUB_CLIENT_ID") // TODO + redirectURL := r.Header.Get(XCallBackHeader) // 指定するコールバック先のURL + var url string + if redirectURL != "" { + // コールバック先明示 + url = fmt.Sprintf("%s?client_id=%s&redirect_uri=%s&scope=user:read", githubOAuthauthorizeURL, clientId, redirectURL) + } else { + url = fmt.Sprintf("%s?client_id=%s&scope=user:read", githubOAuthauthorizeURL, clientId) + } + zap.L().Info(fmt.Sprintf("move to %s", url)) + zap.L().Info(fmt.Sprintf("redirect_uri is %s", redirectURL)) http.Redirect(w, r, url, http.StatusFound) }) @@ -112,8 +118,8 @@ func (s Server) addHandler(r *chi.Mux) { zap.L().Info("set Cookie") // エラーでなければ親ページに返してあげる - zap.L().Info(fmt.Sprintf("move to %s", s.getServerBaseURL(r.URL))) - http.Redirect(w, r, s.getServerBaseURL(r.URL), http.StatusFound) + zap.L().Info(fmt.Sprintf("move to %s", s.BasePath)) + http.Redirect(w, r, s.BasePath, http.StatusFound) zap.L().Info("callback process done") })