From bdb0e8ff04490eb5da6c31b96d901705df07387d Mon Sep 17 00:00:00 2001 From: davidvader Date: Wed, 27 Sep 2023 08:47:23 -0500 Subject: [PATCH 1/2] feat: add ValidateOAuthToken func --- vela/authentication.go | 11 ++++++++++ vela/authentication_test.go | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/vela/authentication.go b/vela/authentication.go index 8641e92..852e2b7 100644 --- a/vela/authentication.go +++ b/vela/authentication.go @@ -231,3 +231,14 @@ func (svc *AuthenticationService) ValidateToken() (*Response, error) { return resp, err } + +// ValidateOAuthToken makes a request to validate user oauth tokens with the Vela server. +func (svc *AuthenticationService) ValidateOAuthToken() (*Response, error) { + // set the API endpoint path we send the request to + u := "/validate-oauth" + + // attempt to validate an oauth token + resp, err := svc.client.Call("GET", u, nil, nil) + + return resp, err +} diff --git a/vela/authentication_test.go b/vela/authentication_test.go index b1b3068..ee8b056 100644 --- a/vela/authentication_test.go +++ b/vela/authentication_test.go @@ -326,3 +326,45 @@ func TestVela_Authentication_ValidateToken_NoToken(t *testing.T) { t.Error("ValidateToken response should be nil") } } + +func TestVela_Authentication_ValidateOAuthToken_200(t *testing.T) { + // setup context + gin.SetMode(gin.TestMode) + + s := httptest.NewServer(server.FakeHandler()) + c, _ := NewClient(s.URL, "", nil) + + c.Authentication.SetTokenAuth("foo") + + // run test + resp, err := c.Authentication.ValidateOAuthToken() + + if err != nil { + t.Errorf("ValidateOAuthToken returned error %v", err) + } + + if resp.StatusCode != http.StatusOK { + t.Errorf("ValidateOAuthToken returned %v, want %v", resp.StatusCode, http.StatusOK) + } +} + +func TestVela_Authentication_ValidateOAuthToken_NoToken(t *testing.T) { + // setup context + gin.SetMode(gin.TestMode) + + s := httptest.NewServer(server.FakeHandler()) + c, _ := NewClient(s.URL, "", nil) + + c.Authentication.SetTokenAuth("") + + // run test + resp, err := c.Authentication.ValidateOAuthToken() + + if err == nil { + t.Error("ValidateOAuthToken should have returned error") + } + + if resp != nil { + t.Error("ValidateOAuthToken response should be nil") + } +} From bbe1af843a37340d8f5251ef39f7078810d2a531 Mon Sep 17 00:00:00 2001 From: davidvader Date: Wed, 27 Sep 2023 11:48:26 -0500 Subject: [PATCH 2/2] fix: update go mod --- go.mod | 10 +++++----- go.sum | 20 ++++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index c5419ae..5c18469 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/buildkite/yaml v0.0.0-20230306222819-0e4e032d4835 github.com/coreos/go-semver v0.3.1 github.com/gin-gonic/gin v1.9.1 - github.com/go-vela/server v0.20.1-0.20230926205857-ff2e943bb81d + github.com/go-vela/server v0.20.1-0.20230927164659-aa6f468e31ca github.com/go-vela/types v0.20.2-0.20230926151838-42959d67d753 github.com/golang-jwt/jwt/v5 v5.0.0 github.com/google/go-cmp v0.5.9 @@ -37,10 +37,10 @@ require ( github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect golang.org/x/arch v0.3.0 // indirect - golang.org/x/crypto v0.12.0 // indirect - golang.org/x/net v0.14.0 // indirect - golang.org/x/sys v0.11.0 // indirect - golang.org/x/text v0.12.0 // indirect + golang.org/x/crypto v0.13.0 // indirect + golang.org/x/net v0.15.0 // indirect + golang.org/x/sys v0.12.0 // indirect + golang.org/x/text v0.13.0 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index c1c591b..0b3b06e 100644 --- a/go.sum +++ b/go.sum @@ -30,8 +30,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= -github.com/go-vela/server v0.20.1-0.20230926205857-ff2e943bb81d h1:FYnItUmMYEf2ek+yXVY9tuAdN3FwAP8oUo/dAR7S+LA= -github.com/go-vela/server v0.20.1-0.20230926205857-ff2e943bb81d/go.mod h1:+NqAFyWoIZ797jYfiXBEnDZt9ECKe/9hfIEeZljUIr0= +github.com/go-vela/server v0.20.1-0.20230927164659-aa6f468e31ca h1:YPastJ21ubppfTOKLyNW4XxBh+1obN0VXWx10usyAfU= +github.com/go-vela/server v0.20.1-0.20230927164659-aa6f468e31ca/go.mod h1:TJjFqQuAgsDfkAZx0c3U3IHHfzaByOjxGBypIvF5TpE= github.com/go-vela/types v0.20.2-0.20230926151838-42959d67d753 h1:kxbNe8B1Y9wo4sk2sW5EHiysNkI+CuZOJT9b3VQhJsQ= github.com/go-vela/types v0.20.2-0.20230926151838-42959d67d753/go.mod h1:1VyxKYQT0twymljVb83j2ljNS26mUfu/YLjQI7LQ4uQ= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= @@ -96,17 +96,17 @@ github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZ golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= -golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= -golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= -golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= +golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck= +golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= +golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= +golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= -golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= -golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=