Skip to content

Commit

Permalink
refactor: move things around a bit in auth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
HandOfGod94 committed Oct 15, 2023
1 parent 5d7c14f commit bb44949
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions pkg/jira_changelog/jira/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,11 @@ func (a *Authenticator) Login(ctx context.Context) error {
return nil
}

func (a *Authenticator) isVerifierPresent(ctx context.Context, args ...any) bool {
return a.verifier != ""
}

func (a *Authenticator) isTokenValid(ctx context.Context, args ...any) bool {
return a.oauthToken != nil && a.oauthToken.Valid()
}

func (a *Authenticator) isOauthContextPresent(ctx context.Context, args ...any) bool {
return a.ctx != nil
func (a *Authenticator) Client() *http.Client {
return a.conf.Client(a.ctx, a.oauthToken)
}

// ActionFuncs
func (a *Authenticator) setupOauthConfig(ctx context.Context, args ...any) error {
a.conf = &oauth2.Config{
ClientID: "OOGf9PTJL0hGGC5hWD17G6OkiGKjO0FG",
Expand Down Expand Up @@ -124,7 +117,7 @@ func (a *Authenticator) exchangeCode(ctx context.Context, args ...any) error {
},
})

// sping up server for callback from RedirectURL and shut it down once we get response
// spin up server for callback from RedirectURL and shut it down once we get response
a.callback = make(chan *oauth2.Token)
mux := http.NewServeMux()
mux.HandleFunc("/gh-jira-changelog/oauth/callback", http.HandlerFunc(a.callbackHandler))
Expand Down Expand Up @@ -154,10 +147,20 @@ func (a *Authenticator) fetchAccessibleResources(ctx context.Context, args ...an
return nil
}

func (a *Authenticator) Client() *http.Client {
return a.conf.Client(a.ctx, a.oauthToken)
// GaurdFuncs
func (a *Authenticator) isVerifierPresent(ctx context.Context, args ...any) bool {
return a.verifier != ""
}

func (a *Authenticator) isTokenValid(ctx context.Context, args ...any) bool {
return a.oauthToken != nil && a.oauthToken.Valid()
}

func (a *Authenticator) isOauthContextPresent(ctx context.Context, args ...any) bool {
return a.ctx != nil
}

// oauth RedirectURL callback handler
func (a *Authenticator) callbackHandler(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
token, err := a.conf.Exchange(a.ctx, code, oauth2.VerifierOption(a.verifier))
Expand Down

0 comments on commit bb44949

Please sign in to comment.