Skip to content

Commit 2c19376

Browse files
authored
Make redirect url configurable (#50)
1 parent 1fe01c5 commit 2c19376

File tree

6 files changed

+23
-19
lines changed

6 files changed

+23
-19
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ The available flags are:
8080
--par enable pushed authorization requests (PAR)
8181
--password string resource owner password credentials grant flow password
8282
--pkce enable proof key for code exchange (PKCE)
83+
--redirect-url string client redirect url (default "http://localhost:9876/callback")
8384
--refresh-token string refresh token
8485
--request-object pass request parameters as jwt
8586
--response-mode string response mode

cmd/oauth2.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ import (
1818
"github.com/spf13/cobra"
1919
)
2020

21-
const (
22-
addr = "localhost:9876"
23-
)
24-
2521
var (
2622
silent bool
2723
)
@@ -45,6 +41,7 @@ func NewOAuth2Cmd() (cmd *OAuth2Cmd) {
4541

4642
cmd.AddCommand(versionCmd)
4743

44+
cmd.PersistentFlags().StringVar(&cconfig.RedirectURL, "redirect-url", "http://localhost:9876/callback", "client redirect url")
4845
cmd.PersistentFlags().StringVar(&cconfig.ClientID, "client-id", "", "client identifier")
4946
cmd.PersistentFlags().StringVar(&cconfig.ClientSecret, "client-secret", "", "client secret")
5047
cmd.PersistentFlags().StringVar(&cconfig.GrantType, "grant-type", "", "grant type")

cmd/oauth2_authorize_code.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (c *OAuth2Cmd) AuthorizationCodeGrantFlow(clientConfig oauth2.ClientConfig,
2525
if clientConfig.PAR {
2626
LogSection("Request PAR")
2727

28-
if parRequest, parResponse, authorizeRequest, codeVerifier, err = oauth2.RequestPAR(context.Background(), addr, clientConfig, serverConfig, hc); err != nil {
28+
if parRequest, parResponse, authorizeRequest, codeVerifier, err = oauth2.RequestPAR(context.Background(), clientConfig, serverConfig, hc); err != nil {
2929
LogRequestAndResponseln(parRequest, err)
3030
return err
3131
}
@@ -41,7 +41,7 @@ func (c *OAuth2Cmd) AuthorizationCodeGrantFlow(clientConfig oauth2.ClientConfig,
4141
} else {
4242
LogSection("Request authorization")
4343

44-
if authorizeRequest, codeVerifier, err = oauth2.RequestAuthorization(addr, clientConfig, serverConfig, hc); err != nil {
44+
if authorizeRequest, codeVerifier, err = oauth2.RequestAuthorization(clientConfig, serverConfig, hc); err != nil {
4545
return err
4646
}
4747

@@ -65,7 +65,7 @@ func (c *OAuth2Cmd) AuthorizationCodeGrantFlow(clientConfig oauth2.ClientConfig,
6565
// callback
6666
callbackStatus := LogAction("Waiting for callback. Go to the browser to authenticate...")
6767

68-
if callbackRequest, err = oauth2.WaitForCallback(clientConfig, serverConfig, addr, hc); err != nil {
68+
if callbackRequest, err = oauth2.WaitForCallback(clientConfig, serverConfig, hc); err != nil {
6969
LogRequestln(callbackRequest)
7070
return err
7171
}
@@ -87,7 +87,7 @@ func (c *OAuth2Cmd) AuthorizationCodeGrantFlow(clientConfig oauth2.ClientConfig,
8787
serverConfig,
8888
hc,
8989
oauth2.WithAuthorizationCode(callbackRequest.Get("code")),
90-
oauth2.WithRedirectURL("http://"+addr+"/callback"),
90+
oauth2.WithRedirectURL(clientConfig.RedirectURL),
9191
oauth2.WithCodeVerifier(codeVerifier),
9292
); err != nil {
9393
LogRequestAndResponseln(tokenRequest, err)

cmd/oauth2_implicit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (c *OAuth2Cmd) ImplicitGrantFlow(clientConfig oauth2.ClientConfig, serverCo
1919
// authorize endpoint
2020
LogSection("Request authorization")
2121

22-
if authorizeRequest, _, err = oauth2.RequestAuthorization(addr, clientConfig, serverConfig, hc); err != nil {
22+
if authorizeRequest, _, err = oauth2.RequestAuthorization(clientConfig, serverConfig, hc); err != nil {
2323
return err
2424
}
2525

@@ -36,7 +36,7 @@ func (c *OAuth2Cmd) ImplicitGrantFlow(clientConfig oauth2.ClientConfig, serverCo
3636
// callback
3737
callbackStatus := LogAction("Waiting for callback. Go to the browser to authenticate...")
3838

39-
if callbackRequest, err = oauth2.WaitForCallback(clientConfig, serverConfig, addr, hc); err != nil {
39+
if callbackRequest, err = oauth2.WaitForCallback(clientConfig, serverConfig, hc); err != nil {
4040
LogRequestln(callbackRequest)
4141
return err
4242
}

internal/oauth2/oauth2.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var CodeChallengeEncoder = base64.RawURLEncoding
5555

5656
type ClientConfig struct {
5757
IssuerURL string
58+
RedirectURL string
5859
GrantType string
5960
ClientID string
6061
ClientSecret string
@@ -85,12 +86,12 @@ type ClientConfig struct {
8586
TLSRootCA string
8687
}
8788

88-
func RequestAuthorization(addr string, cconfig ClientConfig, sconfig ServerConfig, hc *http.Client) (r Request, codeVerifier string, err error) {
89+
func RequestAuthorization(cconfig ClientConfig, sconfig ServerConfig, hc *http.Client) (r Request, codeVerifier string, err error) {
8990
if r.URL, err = url.Parse(sconfig.AuthorizationEndpoint); err != nil {
9091
return r, "", errors.Wrapf(err, "failed to parse authorization endpoint")
9192
}
9293

93-
if codeVerifier, err = r.AuthorizeRequest(addr, cconfig, sconfig, hc); err != nil {
94+
if codeVerifier, err = r.AuthorizeRequest(cconfig, sconfig, hc); err != nil {
9495
return r, "", errors.Wrapf(err, "failed to create authorization request")
9596
}
9697

@@ -108,7 +109,6 @@ type PARResponse struct {
108109

109110
func RequestPAR(
110111
ctx context.Context,
111-
addr string,
112112
cconfig ClientConfig,
113113
sconfig ServerConfig,
114114
hc *http.Client,
@@ -120,7 +120,7 @@ func RequestPAR(
120120
)
121121

122122
// push authorization request to /par
123-
if codeVerifier, err = parRequest.AuthorizeRequest(addr, cconfig, sconfig, hc); err != nil {
123+
if codeVerifier, err = parRequest.AuthorizeRequest(cconfig, sconfig, hc); err != nil {
124124
return parRequest, parResponse, authorizeRequest, "", errors.Wrapf(err, "failed to create authorization request")
125125
}
126126

@@ -183,14 +183,19 @@ func RequestPAR(
183183
return parRequest, parResponse, authorizeRequest, codeVerifier, nil
184184
}
185185

186-
func WaitForCallback(clientConfig ClientConfig, serverConfig ServerConfig, addr string, hc *http.Client) (request Request, err error) {
186+
func WaitForCallback(clientConfig ClientConfig, serverConfig ServerConfig, hc *http.Client) (request Request, err error) {
187187
var (
188-
srv = http.Server{Addr: addr}
188+
srv = http.Server{}
189+
redirectURL *url.URL
189190
signingKey jose.JSONWebKey
190191
encryptionKey jose.JSONWebKey
191192
done = make(chan struct{})
192193
)
193194

195+
if redirectURL, err = url.Parse(clientConfig.RedirectURL); err != nil {
196+
return request, errors.Wrapf(err, "failed to parse redirect url: %s", clientConfig.RedirectURL)
197+
}
198+
194199
if signingKey, err = ReadKey(SigningKey, serverConfig.JWKsURI, hc); err != nil {
195200
return request, errors.Wrapf(err, "failed to read signing key from %s", serverConfig.JWKsURI)
196201
}
@@ -201,7 +206,9 @@ func WaitForCallback(clientConfig ClientConfig, serverConfig ServerConfig, addr
201206
}
202207
}
203208

204-
http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
209+
srv.Addr = redirectURL.Host
210+
211+
http.HandleFunc(redirectURL.Path, func(w http.ResponseWriter, r *http.Request) {
205212
defer func() {
206213
time.AfterFunc(time.Second, func() {
207214
if err := srv.Shutdown(context.Background()); err != nil {

internal/oauth2/request.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ type Request struct {
2626
}
2727

2828
func (r *Request) AuthorizeRequest(
29-
addr string,
3029
cconfig ClientConfig,
3130
sconfig ServerConfig,
3231
hc *http.Client,
3332
) (codeVerifier string, err error) {
3433
r.Form = url.Values{
3534
"client_id": {cconfig.ClientID},
36-
"redirect_uri": {"http://" + addr + "/callback"},
35+
"redirect_uri": {cconfig.RedirectURL},
3736
"state": {shortuuid.New()},
3837
"nonce": {shortuuid.New()},
3938
}

0 commit comments

Comments
 (0)