Skip to content

Commit

Permalink
Add tests for oidc and oauth authentications
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Feb 28, 2024
1 parent 4d9a77a commit 88fbea3
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 6 deletions.
3 changes: 3 additions & 0 deletions tsuru/auth/login_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2024 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package auth

import (
Expand Down
4 changes: 4 additions & 0 deletions tsuru/auth/logout.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package auth

import (
Expand Down
3 changes: 3 additions & 0 deletions tsuru/auth/logout_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2024 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package auth

import (
Expand Down
4 changes: 4 additions & 0 deletions tsuru/auth/native.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package auth

import (
Expand Down
11 changes: 9 additions & 2 deletions tsuru/auth/oauth.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package auth

import (
Expand Down Expand Up @@ -30,7 +34,8 @@ func oauthLogin(ctx *cmd.Context, loginInfo *authTypes.SchemeInfo) error {
}
redirectURL := fmt.Sprintf("http://localhost:%s", port)
authURL := strings.Replace(loginInfo.Data.AuthorizeURL, "__redirect_url__", redirectURL, 1)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
defer func() {
finish <- true
}()
Expand All @@ -55,7 +60,9 @@ func oauthLogin(ctx *cmd.Context, loginInfo *authTypes.SchemeInfo) error {
w.Header().Add("Content-Type", "text/html")
w.Write([]byte(page))
})
server := &http.Server{}
server := &http.Server{
Handler: mux,
}
go server.Serve(l)
err = open(authURL)
if err != nil {
Expand Down
74 changes: 74 additions & 0 deletions tsuru/auth/oauth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2023 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package auth

import (
"bytes"
"net/http"
"net/http/httptest"
"os"
"time"

"github.com/tsuru/tsuru-client/tsuru/config"
"github.com/tsuru/tsuru/cmd"
"github.com/tsuru/tsuru/exec"
"github.com/tsuru/tsuru/fs/fstest"

"github.com/tsuru/tsuru/types/auth"
"gopkg.in/check.v1"
)

type fakeExecutor struct {
DoExecute func(opts exec.ExecuteOptions) error
}

func (f *fakeExecutor) Execute(opts exec.ExecuteOptions) error {
return f.DoExecute(opts)
}

func (s *S) TestOAuthLogin(c *check.C) {

config.SetFileSystem(&fstest.RecordingFs{})

execut = &fakeExecutor{
DoExecute: func(opts exec.ExecuteOptions) error {

go func() {
time.Sleep(time.Second)
_, err := http.Get("http://localhost:41000")
c.Assert(err, check.IsNil)
}()

return nil
},
}

defer func() {
config.ResetFileSystem()
execut = nil
}()

fakeTsuruServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
c.Assert(req.URL.Path, check.Equals, "/1.0/auth/login")
rw.Write([]byte(`{"token":"mytoken"}`))
}))
defer fakeTsuruServer.Close()

os.Setenv("TSURU_TARGET", fakeTsuruServer.URL)

context := &cmd.Context{
Stdout: &bytes.Buffer{},
}

err := oauthLogin(context, &auth.SchemeInfo{
Data: auth.SchemeData{
Port: "41000",
},
})

c.Assert(err, check.IsNil)
tokenV1, err := config.ReadTokenV1()
c.Assert(err, check.IsNil)
c.Assert(tokenV1, check.Equals, "mytoken")
}
11 changes: 9 additions & 2 deletions tsuru/auth/oidc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package auth

import (
Expand Down Expand Up @@ -44,7 +48,8 @@ func oidcLogin(ctx *cmd.Context, loginInfo *authTypes.SchemeInfo) error {

finish := make(chan bool)

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

defer func() {
finish <- true
Expand Down Expand Up @@ -84,7 +89,9 @@ func oidcLogin(ctx *cmd.Context, loginInfo *authTypes.SchemeInfo) error {
fmt.Fprintf(w, callbackPage, successMarkup)

})
server := &http.Server{}
server := &http.Server{
Handler: mux,
}
go server.Serve(l)
err = open(authURL)
if err != nil {
Expand Down
95 changes: 95 additions & 0 deletions tsuru/auth/oidc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2023 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package auth

import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"net/url"
"time"

"github.com/tsuru/tsuru-client/tsuru/config"
"github.com/tsuru/tsuru/cmd"
"github.com/tsuru/tsuru/exec"
"github.com/tsuru/tsuru/fs/fstest"
"golang.org/x/oauth2"

"github.com/tsuru/tsuru/types/auth"
"gopkg.in/check.v1"
)

func (s *S) TestOIDChLogin(c *check.C) {

config.SetFileSystem(&fstest.RecordingFs{})

execut = &fakeExecutor{
DoExecute: func(opts exec.ExecuteOptions) error {

go func() {
time.Sleep(time.Second)
_, err := http.Get("http://localhost:41000/?code=321")
c.Assert(err, check.IsNil)
}()

return nil
},
}

defer func() {
config.ResetFileSystem()
execut = nil
}()

fakeIDP := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
b, err := io.ReadAll(req.Body)
c.Assert(err, check.IsNil)
body, err := url.ParseQuery(string(b))
c.Assert(err, check.IsNil)

c.Assert(body.Get("code"), check.Equals, "321")

rw.Header().Set("Content-Type", "application/json")
rw.Write([]byte(`{"access_token":"mytoken", "refresh_token": "refreshtoken"}`))
}))
defer fakeIDP.Close()

context := &cmd.Context{
Stdout: &bytes.Buffer{},
Stderr: &bytes.Buffer{},
}

err := oidcLogin(context, &auth.SchemeInfo{
Data: auth.SchemeData{
Port: "41000",
TokenURL: fakeIDP.URL,
ClientID: "test-tsuru",
Scopes: []string{"scope1"},
},
})

c.Assert(err, check.IsNil)
tokenV1, err := config.ReadTokenV1()
c.Assert(err, check.IsNil)
c.Assert(tokenV1, check.Equals, "mytoken")

tokenV2, err := config.ReadTokenV2()
c.Assert(err, check.IsNil)
c.Assert(tokenV2, check.DeepEquals, &config.TokenV2{
Scheme: "oidc",
OAuth2Token: &oauth2.Token{
AccessToken: "mytoken",
RefreshToken: "refreshtoken",
},
OAuth2Config: &oauth2.Config{
ClientID: "test-tsuru",
RedirectURL: "http://localhost:41000",
Scopes: []string{"scope1"},
Endpoint: oauth2.Endpoint{
TokenURL: fakeIDP.URL,
},
},
})
}
4 changes: 3 additions & 1 deletion tsuru/auth/open_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

package auth

import "github.com/tsuru/tsuru/exec"
import (
"github.com/tsuru/tsuru/exec"
)

func open(url string) error {
opts := exec.ExecuteOptions{
Expand Down
4 changes: 4 additions & 0 deletions tsuru/auth/open_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package auth

import (
Expand Down
3 changes: 3 additions & 0 deletions tsuru/auth/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2024 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package auth

import (
Expand Down
1 change: 0 additions & 1 deletion tsuru/config/token_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

type TokenV2 struct {
Scheme string `json:"scheme"`
RawToken string `json:"raw_token,omitempty"`
OAuth2Token *oauth2.Token `json:"oauth2_token,omitempty"`
OAuth2Config *oauth2.Config `json:"oauth2_config,omitempty"`
}
Expand Down

0 comments on commit 88fbea3

Please sign in to comment.