Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
refactor: remove signin and signup states
Browse files Browse the repository at this point in the history
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
  • Loading branch information
rodneyosodo committed Jun 4, 2024
1 parent 6f492e9 commit a38e40a
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 85 deletions.
17 changes: 3 additions & 14 deletions ui/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ func MakeHandler(svc ui.Service, r *chi.Mux, instanceID, prefix string, secureCo

for _, provider := range providers {
if provider.IsEnabled() {
r.HandleFunc("/signup/"+provider.Name(), oauth2Handler(oauth2.SignUp, provider))
r.HandleFunc("/signin/"+provider.Name(), oauth2Handler(oauth2.SignIn, provider))
r.HandleFunc("/signin/"+provider.Name(), oauth2Handler(provider))
}
}

Expand Down Expand Up @@ -1122,19 +1121,9 @@ func decodeLogoutRequest(_ context.Context, _ *http.Request) (interface{}, error
return nil, nil
}

func oauth2Handler(state oauth2.State, provider oauth2.Provider) http.HandlerFunc {
func oauth2Handler(provider oauth2.Provider) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var url string
var err error
switch state {
case oauth2.SignIn:
url, err = provider.GenerateSignInURL()
case oauth2.SignUp:
url, err = provider.GenerateSignUpURL()
default:
err = fmt.Errorf("invalid state")
}

url, err := provider.GenerateURL()
if err != nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
Expand Down
2 changes: 1 addition & 1 deletion ui/mocks/repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions ui/oauth2/google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,7 @@ func (cfg *config) IsEnabled() bool {
return cfg.oauth2.ClientID != "" && cfg.oauth2.ClientSecret != ""
}

func (cfg *config) GenerateSignInURL() (string, error) {
return cfg.generateURL(mgoauth2.SignIn.String())
}

func (cfg *config) GenerateSignUpURL() (string, error) {
return cfg.generateURL(mgoauth2.SignUp.String())
}

func (cfg *config) generateURL(state string) (string, error) {
func (cfg *config) GenerateURL() (string, error) {
URL, err := url.Parse(cfg.oauth2.Endpoint.AuthURL)
if err != nil {
return "", fmt.Errorf("failed to parse google auth url: %s", err)
Expand All @@ -72,8 +64,7 @@ func (cfg *config) generateURL(state string) (string, error) {
parameters.Add("access_type", "offline")
// prompt=consent is required to get the refresh token
parameters.Add("prompt", "consent")
// login or register state is prepended to the state to be used in the callback
parameters.Add("state", fmt.Sprintf("%s-%s", state, cfg.state))
parameters.Add("state", cfg.state)
URL.RawQuery = parameters.Encode()

return URL.String(), nil
Expand Down
36 changes: 4 additions & 32 deletions ui/oauth2/mocks/provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 2 additions & 26 deletions ui/oauth2/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,6 @@ type Provider interface {
// IsEnabled returns whether the provider is enabled.
IsEnabled() bool

// GenerateSignInURL generates a URL for the sign-in flow.
GenerateSignInURL() (string, error)

// GenerateSignUpURL generates a URL for the sign-up flow.
GenerateSignUpURL() (string, error)
}

// State is the state of the OAuth2 flow.
type State uint8

const (
// SignIn is the state for the sign-in flow.
SignIn State = iota
// SignUp is the state for the sign-up flow.
SignUp
)

func (s State) String() string {
switch s {
case SignIn:
return "signin"
case SignUp:
return "signup"
default:
return "unknown"
}
// GenerateURL generates a URL for the sign-in flow and sign-up flow.
GenerateURL() (string, error)
}
2 changes: 1 addition & 1 deletion ui/web/templates/registration.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h2>Register</h2>
<div class="text-center text-light">
<p>or sign up with:</p>
<button type="button" class="btn btn-link btn-floating mx-1">
<a href="{{ printf "%s/signup/%s" pathPrefix $c.Name }}">
<a href="{{ printf "%s/signin/%s" pathPrefix $c.Name }}">
<i class="fab {{ $c.Icon }}"></i>
</a>
</button>
Expand Down

0 comments on commit a38e40a

Please sign in to comment.