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

Add OTP support to grant-type password #1174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions pkg/oidc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
GetAuthCodeURL(in AuthCodeURLInput) string
ExchangeAuthCode(ctx context.Context, in ExchangeAuthCodeInput) (*oidc.TokenSet, error)
GetTokenByAuthCode(ctx context.Context, in GetTokenByAuthCodeInput, localServerReadyChan chan<- string) (*oidc.TokenSet, error)
GetTokenByROPC(ctx context.Context, username, password string) (*oidc.TokenSet, error)
GetTokenByROPC(ctx context.Context, username, password string, otp string) (*oidc.TokenSet, error)
GetDeviceAuthorization(ctx context.Context) (*oauth2dev.AuthorizationResponse, error)
ExchangeDeviceCode(ctx context.Context, authResponse *oauth2dev.AuthorizationResponse) (*oidc.TokenSet, error)
Refresh(ctx context.Context, refreshToken string) (*oidc.TokenSet, error)
Expand Down Expand Up @@ -147,9 +147,9 @@
}

// GetTokenByROPC performs the resource owner password credentials flow.
func (c *client) GetTokenByROPC(ctx context.Context, username, password string) (*oidc.TokenSet, error) {
func (c *client) GetTokenByROPC(ctx context.Context, username, password string, otp string) (*oidc.TokenSet, error) {
ctx = c.wrapContext(ctx)
token, err := c.oauth2Config.PasswordCredentialsToken(ctx, username, password)
token, err := c.oauth2Config.PasswordCredentialsToken(ctx, username, password, otp)

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, linux, amd64, 0)

too many arguments in call to c.oauth2Config.PasswordCredentialsToken

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, linux, arm64)

too many arguments in call to c.oauth2Config.PasswordCredentialsToken

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / integration-test (ubuntu-latest)

too many arguments in call to c.oauth2Config.PasswordCredentialsToken

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, linux, arm)

too many arguments in call to c.oauth2Config.PasswordCredentialsToken

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / integration-test (macos-latest)

too many arguments in call to c.oauth2Config.PasswordCredentialsToken

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, linux, ppc64le)

too many arguments in call to c.oauth2Config.PasswordCredentialsToken

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / test

too many arguments in call to c.oauth2Config.PasswordCredentialsToken

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / build (macos-latest, darwin, amd64, 1)

too many arguments in call to c.oauth2Config.PasswordCredentialsToken

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / integration-test (windows-latest)

too many arguments in call to c.oauth2Config.PasswordCredentialsToken

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / build (macos-latest, darwin, arm64, 1)

too many arguments in call to c.oauth2Config.PasswordCredentialsToken

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / check / lint

too many arguments in call to c.oauth2Config.PasswordCredentialsToken

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / check / lint

too many arguments in call to c.oauth2Config.PasswordCredentialsToken

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / check / lint

too many arguments in call to c.oauth2Config.PasswordCredentialsToken

Check failure on line 152 in pkg/oidc/client/client.go

View workflow job for this annotation

GitHub Actions / check / lint

too many arguments in call to c.oauth2Config.PasswordCredentialsToken
if err != nil {
return nil, fmt.Errorf("resource owner password credentials flow error: %w", err)
}
Expand Down
12 changes: 11 additions & 1 deletion pkg/usecases/authentication/ropc/ropc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (

const usernamePrompt = "Username: "
const passwordPrompt = "Password: "
const OTPPrompt = "OTP (if set): "

type Option struct {
Username string
Password string // If empty, read a password using Reader.ReadPassword()
OTP string // If empty, read a password using Reader.ReadPassword()
}

// ROPC provides the resource owner password credentials flow.
Expand All @@ -40,7 +42,15 @@ func (u *ROPC) Do(ctx context.Context, in *Option, oidcClient client.Interface)
return nil, fmt.Errorf("could not read a password: %w", err)
}
}
tokenSet, err := oidcClient.GetTokenByROPC(ctx, in.Username, in.Password)
if in.OTP == "" {
var err error
in.OTP, err = u.Reader.ReadPassword(OTPPrompt)
if err != nil {
return nil, fmt.Errorf("could not read an OTP: %w", err)
}
}

tokenSet, err := oidcClient.GetTokenByROPC(ctx, in.Username, in.Password, in.OTP)
if err != nil {
return nil, fmt.Errorf("resource owner password credentials flow error: %w", err)
}
Expand Down
Loading