Skip to content

Commit f7c1024

Browse files
authored
feat: client-side PKCE take 3 (#4078)
* feat: client-side PKCE This change introduces a new configuration for OIDC providers: pkce with values auto (default), never, force. When auto is specified or the field is omitted, Kratos will perform autodiscovery and perform PKCE when the server advertises support for it. This requires the issuer_url to be set for the provider. never completely disables PKCE support. This is only theoretically useful: when a provider advertises PKCE support but doesn't actually implement it. force always sends a PKCE challenge in the initial redirect URL, regardless of what the provider advertises. This setting is useful when the provider offers PKCE but doesn't advertise it in his ./well-known/openid-configuration. Important: When setting pkce: force, you must whitelist a different return URL for your OAuth2 client in the provider's configuration. Instead of <base-url>/self-service/methods/oidc/callback/<provider>, you must use <base-url>/self-service/methods/oidc/callback (note missing last path segment). This is to enable the use of the same OAuth client ID+secret when configuring several Kratos OIDC providers, without having to whitelist individual redirect_uris for each Kratos provider config. * chore: regenerate SDK, bump DB versions, cleanup tool install * chore: get final organization ID from provider config during registration and login * chore: fixup OIDC function signatures and improve tests
1 parent 5592029 commit f7c1024

File tree

70 files changed

+4301
-517
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+4301
-517
lines changed

.github/workflows/ci.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- sdk-generate
2929
services:
3030
postgres:
31-
image: postgres:11.8
31+
image: postgres:14
3232
env:
3333
POSTGRES_DB: postgres
3434
POSTGRES_PASSWORD: test
@@ -111,15 +111,15 @@ jobs:
111111
- sdk-generate
112112
services:
113113
postgres:
114-
image: postgres:11.8
114+
image: postgres:14
115115
env:
116116
POSTGRES_DB: postgres
117117
POSTGRES_PASSWORD: test
118118
POSTGRES_USER: test
119119
ports:
120120
- 5432:5432
121121
mysql:
122-
image: mysql:5.7
122+
image: mysql:8.0
123123
env:
124124
MYSQL_ROOT_PASSWORD: test
125125
ports:
@@ -222,15 +222,15 @@ jobs:
222222
- sdk-generate
223223
services:
224224
postgres:
225-
image: postgres:11.8
225+
image: postgres:14
226226
env:
227227
POSTGRES_DB: postgres
228228
POSTGRES_PASSWORD: test
229229
POSTGRES_USER: test
230230
ports:
231231
- 5432:5432
232232
mysql:
233-
image: mysql:5.7
233+
image: mysql:8.0
234234
env:
235235
MYSQL_ROOT_PASSWORD: test
236236
ports:

Makefile

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ $(call make-lint-dependency)
3232
echo "deprecated usage, use docs/cli instead"
3333
go build -o .bin/clidoc ./cmd/clidoc/.
3434

35-
.PHONY: .bin/yq
36-
.bin/yq:
37-
go build -o .bin/yq github.com/mikefarah/yq/v4
35+
.bin/yq: Makefile
36+
GOBIN=$(PWD)/.bin go install github.com/mikefarah/yq/[email protected]
3837

3938
.PHONY: docs/cli
4039
docs/cli:
@@ -58,17 +57,31 @@ docs/swagger:
5857
curl https://raw.githubusercontent.com/ory/meta/master/install.sh | bash -s -- -b .bin ory v0.2.2
5958
touch -a -m .bin/ory
6059

60+
.bin/buf: Makefile
61+
curl -sSL \
62+
"https://github.com/bufbuild/buf/releases/download/v1.39.0/buf-$(shell uname -s)-$(shell uname -m).tar.gz" | \
63+
tar -xvzf - -C ".bin/" --strip-components=2 buf/bin/buf buf/bin/protoc-gen-buf-breaking buf/bin/protoc-gen-buf-lint
64+
touch -a -m .bin/buf
65+
6166
.PHONY: lint
6267
lint: .bin/golangci-lint
63-
golangci-lint run -v --timeout 10m ./...
68+
.bin/golangci-lint run -v --timeout 10m ./...
69+
.bin/buf lint
6470

6571
.PHONY: mocks
6672
mocks: .bin/mockgen
6773
mockgen -mock_names Manager=MockLoginExecutorDependencies -package internal -destination internal/hook_login_executor_dependencies.go github.com/ory/kratos/selfservice loginExecutorDependencies
6874

75+
.PHONY: proto
76+
proto: gen/oidc/v1/state.pb.go
77+
78+
gen/oidc/v1/state.pb.go: proto/oidc/v1/state.proto buf.yaml buf.gen.yaml .bin/buf .bin/goimports
79+
.bin/buf generate
80+
.bin/goimports -w gen/
81+
6982
.PHONY: install
7083
install:
71-
GO111MODULE=on go install -tags sqlite .
84+
go install -tags sqlite .
7285

7386
.PHONY: test-resetdb
7487
test-resetdb:
@@ -163,11 +176,12 @@ authors: # updates the AUTHORS file
163176

164177
# Formats the code
165178
.PHONY: format
166-
format: .bin/goimports .bin/ory node_modules
167-
.bin/ory dev headers copyright --exclude=internal/httpclient --exclude=internal/client-go --exclude test/e2e/proxy/node_modules --exclude test/e2e/node_modules --exclude node_modules
179+
format: .bin/goimports .bin/ory node_modules .bin/buf
180+
.bin/ory dev headers copyright --exclude=gen --exclude=internal/httpclient --exclude=internal/client-go --exclude test/e2e/proxy/node_modules --exclude test/e2e/node_modules --exclude node_modules
168181
goimports -w -local github.com/ory .
169182
npm exec -- prettier --write 'test/e2e/**/*{.ts,.js}'
170183
npm exec -- prettier --write '.github'
184+
.bin/buf format --write
171185

172186
# Build local docker image
173187
.PHONY: docker

buf.gen.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: v2
2+
managed:
3+
enabled: true
4+
override:
5+
- file_option: go_package_prefix
6+
value: github.com/ory/kratos
7+
plugins:
8+
- remote: buf.build/protocolbuffers/go
9+
out: gen
10+
opt: paths=source_relative
11+
inputs:
12+
- directory: proto

buf.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: v2
2+
modules:
3+
- path: proto
4+
lint:
5+
use:
6+
- DEFAULT
7+
breaking:
8+
use:
9+
- FILE

cipher/chacha20.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"crypto/rand"
99
"encoding/hex"
1010
"io"
11+
"math"
1112

1213
"github.com/pkg/errors"
1314
"golang.org/x/crypto/chacha20poly1305"
@@ -43,6 +44,11 @@ func (c *XChaCha20Poly1305) Encrypt(ctx context.Context, message []byte) (string
4344
return "", herodot.ErrInternalServerError.WithWrap(err).WithReason("Unable to generate key")
4445
}
4546

47+
// Make sure the size calculation does not overflow.
48+
if len(message) > math.MaxInt-aead.NonceSize()-aead.Overhead() {
49+
return "", errors.WithStack(herodot.ErrInternalServerError.WithReason("plaintext too large"))
50+
}
51+
4652
nonce := make([]byte, aead.NonceSize(), aead.NonceSize()+len(message)+aead.Overhead())
4753
_, err = io.ReadFull(rand.Reader, nonce)
4854
if err != nil {

cmd/identities/get_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package identities_test
55

66
import (
77
"context"
8-
"encoding/hex"
98
"encoding/json"
109
"testing"
1110

@@ -63,10 +62,12 @@ func TestGetCmd(t *testing.T) {
6362
return out
6463
}
6564
transform := func(token string) string {
66-
if !encrypt {
67-
return token
65+
if encrypt {
66+
s, err := reg.Cipher(context.Background()).Encrypt(context.Background(), []byte(token))
67+
require.NoError(t, err)
68+
return s
6869
}
69-
return hex.EncodeToString([]byte(token))
70+
return token
7071
}
7172
return identity.Credentials{
7273
Type: identity.CredentialsTypeOIDC,

cmd/identities/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/ory/kratos/internal/testhelpers"
2222
)
2323

24-
func setup(t *testing.T, newCmd func() *cobra.Command) (driver.Registry, *cmdx.CommandExecuter) {
24+
func setup(t *testing.T, newCmd func() *cobra.Command) (*driver.RegistryDefault, *cmdx.CommandExecuter) {
2525
conf, reg := internal.NewFastRegistryWithMocks(t)
2626
_, admin := testhelpers.NewKratosServerWithCSRF(t, reg)
2727
testhelpers.SetDefaultIdentitySchema(conf, "file://./stubs/identity.schema.json")

gen/oidc/v1/state.pb.go

Lines changed: 183 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)