Skip to content

Commit

Permalink
use and show redirect basepath
Browse files Browse the repository at this point in the history
  • Loading branch information
azuki774 committed Oct 26, 2024
1 parent 6b4bcf3 commit 4ab7a60
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/go-authenticator/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ to quickly create a Cobra application.`,
Port: serveConfig.Port,
Authenticator: &authenticator,
CookieLife: serveConfig.TokenLifeTime,
ServerBaseURL: os.Getenv("SERVER_BASEURL"),
BasePath: "/",
}

if err := server.Serve(); err != nil {
Expand Down
1 change: 1 addition & 0 deletions deployment/default.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# OVERRIDE this file before running container.
conf-version = 1

# For CI Sample
Expand Down
4 changes: 3 additions & 1 deletion docs/memo.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
- Basic認証を受け付け、認証があっていればJWTトークンをCookieで返す。

## GET /login_page
- ログイン方法を選択
- github oauth2認証は繊維
- Header: `X-Callback-URL` に値を入れると、GitHub oauth2 認証時に `redirect_uri` として値を連携する。
- 連携成功後、このURLにコールバックされる。

## GET /callback/github?code={code}
- githubログイン後の oauth2 callback 先
12 changes: 9 additions & 3 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
Expand All @@ -17,7 +18,7 @@ type Server struct {
Port int
Authenticator Authenticator
CookieLife int // token_life, cookie: max-age
ServerBaseURL string // 認証のリダイレクト後、戻って来るURLを指定 ex. http://localhost:8888/
BasePath string // BasePath for redirect_url
}

type Authenticator interface {
Expand All @@ -28,6 +29,11 @@ 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"))
Expand Down Expand Up @@ -106,8 +112,8 @@ func (s Server) addHandler(r *chi.Mux) {
zap.L().Info("set Cookie")

// エラーでなければ親ページに返してあげる
zap.L().Info(fmt.Sprintf("move to %s", s.ServerBaseURL))
http.Redirect(w, r, s.ServerBaseURL, http.StatusFound)
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("callback process done")
})
Expand Down

0 comments on commit 4ab7a60

Please sign in to comment.