Skip to content

Commit

Permalink
Merge pull request #8 from azuki774/develop-241026
Browse files Browse the repository at this point in the history
use and show redirect basepath
  • Loading branch information
azuki774 authored Oct 26, 2024
2 parents 6b4bcf3 + ab26448 commit 77403dd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 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 先
22 changes: 17 additions & 5 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ 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
CookieLife int // token_life, cookie: max-age
ServerBaseURL string // 認証のリダイレクト後、戻って来るURLを指定 ex. http://localhost:8888/
BasePath string // BasePath for redirect_url
}

type Authenticator interface {
Expand Down Expand Up @@ -67,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)
})

Expand Down Expand Up @@ -106,8 +118,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.BasePath))
http.Redirect(w, r, s.BasePath, http.StatusFound)

zap.L().Info("callback process done")
})
Expand Down

0 comments on commit 77403dd

Please sign in to comment.