Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement - Add option to add a state to an auth code url request #485

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion apps/confidential/confidential.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func New(authority, clientID string, cred Credential, options ...Option) (Client

// authCodeURLOptions contains options for AuthCodeURL
type authCodeURLOptions struct {
claims, loginHint, tenantID, domainHint string
claims, loginHint, tenantID, domainHint, state string
}

// AuthCodeURLOption is implemented by options for AuthCodeURL
Expand All @@ -363,12 +363,36 @@ func (cca Client) AuthCodeURL(ctx context.Context, clientID, redirectURI string,
if err != nil {
return "", err
}
ap.State = o.state
ap.Claims = o.claims
ap.LoginHint = o.loginHint
ap.DomainHint = o.domainHint
return cca.base.AuthCodeURL(ctx, clientID, redirectURI, scopes, ap)
}

// WithState adds a user-generated state to the request.
func WithState(state string) interface {
AuthCodeURLOption
options.CallOption
} {
return struct {
AuthCodeURLOption
options.CallOption
}{
CallOption: options.NewCallOption(
func(a any) error {
switch t := a.(type) {
case *authCodeURLOptions:
t.state = state
default:
return fmt.Errorf("unexpected options type %T", a)
}
return nil
},
),
}
}

// WithLoginHint pre-populates the login prompt with a username.
func WithLoginHint(username string) interface {
AuthCodeURLOption
Expand Down
40 changes: 40 additions & 0 deletions apps/confidential/confidential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,46 @@ func TestWithLoginHint(t *testing.T) {
}
}

func TestWithState(t *testing.T) {
state := "abc-123-secure-string"
cred, err := NewCredFromSecret(fakeSecret)
if err != nil {
t.Fatal(err)
}
client, err := New(fakeAuthority, fakeClientID, cred, WithHTTPClient(&errorClient{}))
if err != nil {
t.Fatal(err)
}
client.base.Token.Resolver = &fake.ResolveEndpoints{}
for _, expectState := range []bool{true, false} {
t.Run(fmt.Sprint(expectState), func(t *testing.T) {
opts := []AuthCodeURLOption{}
if expectState {
opts = append(opts, WithState(state))
}
u, err := client.AuthCodeURL(context.Background(), "id", localhost, tokenScope, opts...)
if err != nil {
t.Fatal(err)
}
parsed, err := url.Parse(u)
if err != nil {
t.Fatal(err)
}
if !parsed.Query().Has("state") {
if !expectState {
return
}
t.Fatal("expected a state")
} else if !expectState {
t.Fatal("expected no state")
}
if actual := parsed.Query()["state"]; len(actual) != 1 || actual[0] != state {
t.Fatalf(`unexpected state "%v"`, actual)
}
})
}
}

func TestWithDomainHint(t *testing.T) {
domain := "contoso.com"
cred, err := NewCredFromSecret(fakeSecret)
Expand Down