Skip to content

Commit 546ee3d

Browse files
committed
feat: improve state generation logic
1 parent 4159b93 commit 546ee3d

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

selfservice/strategy/oidc/strategy.go

+7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package oidc
33
import (
44
"bytes"
55
"context"
6+
"encoding/base64"
67
"encoding/json"
8+
"fmt"
79
"net/http"
810
"path/filepath"
911
"strings"
@@ -114,6 +116,11 @@ type authCodeContainer struct {
114116
Traits json.RawMessage `json:"traits"`
115117
}
116118

119+
func generateState(flowID string) string {
120+
state := x.NewUUID().String()
121+
return base64.RawURLEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", flowID, state)))
122+
}
123+
117124
func (s *Strategy) CountActiveFirstFactorCredentials(cc map[identity.CredentialsType]identity.Credentials) (count int, err error) {
118125
for _, c := range cc {
119126
if c.Type == s.ID() && gjson.ValidBytes(c.Config) {

selfservice/strategy/oidc/strategy_login.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (s *Strategy) Login(w http.ResponseWriter, r *http.Request, f *login.Flow,
167167
return
168168
}
169169

170-
state := x.NewUUID().String()
170+
state := generateState(f.ID.String())
171171
if err := s.d.ContinuityManager().Pause(r.Context(), w, r, sessionName,
172172
continuity.WithPayload(&authCodeContainer{
173173
State: state,

selfservice/strategy/oidc/strategy_registration.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (s *Strategy) Register(w http.ResponseWriter, r *http.Request, f *registrat
141141
return errors.WithStack(registration.ErrAlreadyLoggedIn)
142142
}
143143

144-
state := x.NewUUID().String()
144+
state := generateState(f.ID.String())
145145
if err := s.d.ContinuityManager().Pause(r.Context(), w, r, sessionName,
146146
continuity.WithPayload(&authCodeContainer{
147147
State: state,

selfservice/strategy/oidc/strategy_settings.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func (s *Strategy) initLinkProvider(w http.ResponseWriter, r *http.Request, ctxU
336336
return s.handleSettingsError(w, r, ctxUpdate, p, err)
337337
}
338338

339-
state := x.NewUUID().String()
339+
state := generateState(ctxUpdate.Flow.ID.String())
340340
if err := s.d.ContinuityManager().Pause(r.Context(), w, r, sessionName,
341341
continuity.WithPayload(&authCodeContainer{
342342
State: state,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package oidc
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/ory/kratos/x"
9+
)
10+
11+
func TestGenerateState(t *testing.T) {
12+
state := generateState(x.NewUUID().String())
13+
assert.NotEmpty(t, state)
14+
t.Logf("state: %s", state)
15+
}

0 commit comments

Comments
 (0)