Skip to content

Commit

Permalink
fix: login endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pandatix committed Apr 15, 2024
1 parent bdd6ba3 commit f3e57e8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 40 deletions.
8 changes: 4 additions & 4 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func NewClient(url, nonce, session, apiKey string) *Client {
return &Client{
sub: &http.Client{
Jar: jar,
// Don't follow redirections
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
},
// // Don't follow redirections
// CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
// return http.ErrUseLastResponse
// },
},
url: url,
nonce: nonce,
Expand Down
13 changes: 2 additions & 11 deletions api/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"net/http"
"net/url"

"github.com/pkg/errors"
)

type LoginParams struct {
Expand Down Expand Up @@ -35,7 +33,7 @@ func (client *Client) Login(params *LoginParams, opts ...Option) error {
}
defer res.Body.Close()

if res.StatusCode != http.StatusFound {
if res.StatusCode != http.StatusOK {
return fmt.Errorf("CTFd responded with status code %d", res.StatusCode)
}

Expand All @@ -45,12 +43,5 @@ func (client *Client) Login(params *LoginParams, opts ...Option) error {
return err
}
client.nonce = nonce

for _, cookie := range res.Cookies() {
if cookie.Name == "session" {
client.session = cookie.Value
return nil
}
}
return errors.New("session cookie not found")
return nil
}
13 changes: 2 additions & 11 deletions api/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package api
import (
"fmt"
"net/http"

"github.com/pkg/errors"
)

func (client *Client) Logout(opts ...Option) error {
Expand All @@ -15,7 +13,7 @@ func (client *Client) Logout(opts ...Option) error {
}
defer res.Body.Close()

if res.StatusCode != http.StatusFound {
if res.StatusCode != http.StatusOK {
return fmt.Errorf("CTFd responded with status code %d", res.StatusCode)
}

Expand All @@ -25,12 +23,5 @@ func (client *Client) Logout(opts ...Option) error {
return err
}
client.nonce = nonce

for _, cookie := range res.Cookies() {
if cookie.Name == "session" {
client.session = cookie.Value
break
}
}
return errors.New("session cookie not found")
return nil
}
14 changes: 1 addition & 13 deletions api/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"bytes"
"errors"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -30,22 +29,11 @@ func (client *Client) Register(params *RegisterParams, opts ...Option) error {
}
defer res.Body.Close()

if res.StatusCode != http.StatusFound {
if res.StatusCode != http.StatusOK {
return fmt.Errorf("CTFd responded with status code %d, which could be due to email reuse", res.StatusCode)
}

// Update session to track user then fetch nonce for later API calls
cookieFound := false
for _, cookie := range res.Cookies() {
if cookie.Name == "session" {
client.session = cookie.Value
cookieFound = true
break
}
}
if !cookieFound {
return errors.New("session cookie not found, may be due to server misconfiguration (not setup yet) or API instability")
}
req, _ = http.NewRequest(http.MethodGet, "/", nil)
res, err = client.Do(req)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (client *Client) Reset(params *ResetParams, opts ...Option) error {
}
defer res.Body.Close()

if res.StatusCode != http.StatusFound {
if res.StatusCode != http.StatusOK {
return fmt.Errorf("CTFd responded with status code %d", res.StatusCode)
}

Expand Down
15 changes: 15 additions & 0 deletions api/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ func Test_F_Setup(t *testing.T) {
}
client.SetAPIKey(*token.Value)

// 1d. Logout because we don't know what could happen with a mouse on the UI
err = client.Logout()
if !assert.Nil(err, "got error: %s", err) {
return
}

// 1e. Relog :)
err = client.Login(&api.LoginParams{
Name: "ctfer",
Password: "password",
})
if !assert.Nil(err, "got error: %s", err) {
return
}

// 2. Create a challenge
chall, err := client.PostChallenges(&api.PostChallengesParams{
Name: "Stealing data",
Expand Down

0 comments on commit f3e57e8

Please sign in to comment.