Skip to content

Commit

Permalink
fix: post-merge linter fixes (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
wass3r authored Jan 22, 2021
1 parent 6053428 commit a88a5d8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
30 changes: 27 additions & 3 deletions api/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ package api
import (
"encoding/base64"
"fmt"
"net/http"

"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/token"
"github.com/go-vela/server/source"
"github.com/go-vela/server/util"
"github.com/go-vela/types"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"net/http"

"github.com/go-vela/types/library"

Expand Down Expand Up @@ -211,11 +212,34 @@ func AuthenticateType(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, r)
}

// swagger:operation POST /authenticate/token authenticate PostAuthenticate
//
// Authenticate to Vela via personal access token.
//
// ---
// x-success_http_code: '200'
// produces:
// - application/json
// responses:
// '200':
// description: Successfully authenticated
// schema:
// type: string
// responses:
// '401':
// description: Unable to authenticate
// schema:
// type: string
// '503':
// description: Service unavailable
// schema:
// type: string

// AuthenticateToken represents the API handler to
// process a user logging in using PAT to Vela from
// the API
// the API.
func AuthenticateToken(c *gin.Context) {
newUser, err := source.FromContext(c).AuthenticateToken(c.Writer, c.Request)
newUser, err := source.FromContext(c).AuthenticateToken(c.Request)
if err != nil {
retErr := fmt.Errorf("unable to authenticate user: %w", err)

Expand Down
10 changes: 8 additions & 2 deletions source/github/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package github

import (
"context"
"errors"
"fmt"
"net/http"

Expand Down Expand Up @@ -54,7 +55,9 @@ func (c *client) Login(w http.ResponseWriter, r *http.Request) (string, error) {
return oAuthState, nil
}

// Authenticate completes the authentication workflow for the session and returns the remote user details.
// Authenticate completes the authentication workflow for the session
// and returns the remote user details.
// nolint:lll // long struct references
func (c *client) Authenticate(w http.ResponseWriter, r *http.Request, oAuthState string) (*library.User, error) {
logrus.Trace("Authenticating user")

Expand Down Expand Up @@ -96,10 +99,13 @@ func (c *client) Authenticate(w http.ResponseWriter, r *http.Request, oAuthState

// AuthenticateToken completes the authentication workflow
// for the session and returns the remote user details.
func (c *client) AuthenticateToken(w http.ResponseWriter, r *http.Request) (*library.User, error) {
func (c *client) AuthenticateToken(r *http.Request) (*library.User, error) {
logrus.Trace("Authenticating user via token")

token := r.Header.Get("Token")
if len(token) == 0 {
return nil, errors.New("no token provided")
}

u, err := c.Authorize(token)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions source/github/authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func TestGithub_Authenticate_Token(t *testing.T) {
client, _ := NewTest(s.URL)

// run test
got, err := client.AuthenticateToken(context.Writer, context.Request)
got, err := client.AuthenticateToken(context.Request)

if resp.Code != http.StatusOK {
t.Errorf("Authenticate returned %v, want %v", resp.Code, http.StatusOK)
Expand Down Expand Up @@ -369,7 +369,7 @@ func TestGithub_Authenticate_Invalid_Token(t *testing.T) {
client, _ := NewTest(s.URL)

// run test
got, err := client.AuthenticateToken(context.Writer, context.Request)
got, err := client.AuthenticateToken(context.Request)

if resp.Code != http.StatusOK {
t.Errorf("Authenticate returned %v, want %v", resp.Code, http.StatusOK)
Expand Down
2 changes: 1 addition & 1 deletion source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Service interface {

// AuthenticateToken defines a function that completes
// the OAuth workflow for the session using PAT Token
AuthenticateToken(http.ResponseWriter, *http.Request) (*library.User, error)
AuthenticateToken(*http.Request) (*library.User, error)

// Login defines a function that begins
// the OAuth workflow for the session.
Expand Down

0 comments on commit a88a5d8

Please sign in to comment.