Skip to content

Commit 1777dd0

Browse files
authored
Add support for tls callback (#99)
1 parent fedb9f6 commit 1777dd0

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

cmd/oauth2.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ func NewOAuth2Cmd(version, commit, date string) (cmd *OAuth2Cmd) {
7272
cmd.PersistentFlags().StringVar(&cconfig.TLSCert, "tls-cert", "", "path to tls cert pem file")
7373
cmd.PersistentFlags().StringVar(&cconfig.TLSKey, "tls-key", "", "path to tls key pem file")
7474
cmd.PersistentFlags().StringVar(&cconfig.TLSRootCA, "tls-root-ca", "", "path to tls root ca pem file")
75+
cmd.PersistentFlags().StringVar(&cconfig.CallbackTLSCert, "callback-tls-cert", "", "path to callback tls cert pem file")
76+
cmd.PersistentFlags().StringVar(&cconfig.CallbackTLSKey, "callback-tls-key", "", "path to callback tls key pem file")
7577
cmd.PersistentFlags().DurationVar(&cconfig.HTTPTimeout, "http-timeout", time.Minute, "http client timeout")
7678
cmd.PersistentFlags().DurationVar(&cconfig.BrowserTimeout, "browser-timeout", 10*time.Minute, "browser timeout")
7779
cmd.PersistentFlags().BoolVar(&cconfig.Insecure, "insecure", false, "allow insecure connections")

docs/examples.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,3 +594,23 @@ oauth2c https://oauth2c.us.authz.cloudentity.io/oauth2c/demo \
594594
--rar '[{"type":"payment_initiation","locations":["https://example.com/payments"],"instructedAmount":{"currency":"EUR","amount":"123.50"},"creditorName":"Merchant A","creditorAccount":{"bic":"ABCIDEFFXXX","iban":"DE02100100109307118603"},"remittanceInformationUnstructured":"Ref Number Merchant"}]'
595595
```
596596
</details>
597+
598+
## Miscellaneous
599+
600+
### Using HTTPs for Callback URL
601+
602+
You can use `--callback-tls-cert` and `--callback-tls-key` flags to specify a
603+
TLS certificate and key for the HTTPs callback redirect URL.
604+
605+
```sh
606+
oauth2c https://oauth2c.us.authz.cloudentity.io/oauth2c/demo \
607+
--client-id cauktionbud6q8ftlqq0 \
608+
--client-secret HCwQ5uuUWBRHd04ivjX5Kl0Rz8zxMOekeLtqzki0GPc \
609+
--response-types code \
610+
--response-mode query \
611+
--grant-type authorization_code \
612+
--auth-method client_secret_basic \
613+
--redirect-url https://localhost:9876/callback \
614+
--callback-tls-cert https://raw.githubusercontent.com/cloudentity/oauth2c/master/data/cert.pem \
615+
--callback-tls-key https://raw.githubusercontent.com/cloudentity/oauth2c/master/data/key.pem
616+
```

internal/oauth2/oauth2.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package oauth2
22

33
import (
44
"context"
5+
"crypto/tls"
56
"encoding/base64"
67
"encoding/json"
78
"fmt"
@@ -83,6 +84,8 @@ type ClientConfig struct {
8384
TLSCert string `validate:"omitempty,uri"`
8485
TLSKey string `validate:"omitempty,uri"`
8586
TLSRootCA string `validate:"omitempty,uri"`
87+
CallbackTLSCert string `validate:"omitempty,uri"`
88+
CallbackTLSKey string `validate:"omitempty,uri"`
8689
HTTPTimeout time.Duration
8790
BrowserTimeout time.Duration
8891
DPoP bool
@@ -192,6 +195,7 @@ func WaitForCallback(clientConfig ClientConfig, serverConfig ServerConfig, hc *h
192195
var (
193196
srv = http.Server{}
194197
redirectURL *url.URL
198+
cert tls.Certificate
195199
done = make(chan struct{})
196200
)
197201

@@ -205,6 +209,16 @@ func WaitForCallback(clientConfig ClientConfig, serverConfig ServerConfig, hc *h
205209
redirectURL.Path = "/"
206210
}
207211

212+
if redirectURL.Scheme == "https" {
213+
if cert, err = ReadKeyPair(clientConfig.CallbackTLSCert, clientConfig.CallbackTLSKey, hc); err != nil {
214+
return request, errors.Wrapf(err, "failed to read callback tls key pair")
215+
}
216+
217+
srv.TLSConfig = &tls.Config{
218+
Certificates: []tls.Certificate{cert},
219+
}
220+
}
221+
208222
http.HandleFunc(redirectURL.Path, func(w http.ResponseWriter, r *http.Request) {
209223
defer func() {
210224
time.AfterFunc(time.Second, func() {
@@ -273,8 +287,14 @@ func WaitForCallback(clientConfig ClientConfig, serverConfig ServerConfig, hc *h
273287
go func() {
274288
defer close(done)
275289

276-
if serr := srv.ListenAndServe(); serr != http.ErrServerClosed {
277-
err = serr
290+
if redirectURL.Scheme == "https" {
291+
if serr := srv.ListenAndServeTLS("", ""); serr != http.ErrServerClosed {
292+
err = serr
293+
}
294+
} else {
295+
if serr := srv.ListenAndServe(); serr != http.ErrServerClosed {
296+
err = serr
297+
}
278298
}
279299
}()
280300

0 commit comments

Comments
 (0)