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

fix: post-merge linter fixes #257

Merged
merged 2 commits into from
Jan 22, 2021
Merged
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
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