Skip to content

Commit b178ab9

Browse files
committed
Add support for password protected sessions for codegame-share
1 parent 13b9637 commit b178ab9

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

cmd/session_import.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"time"
78

89
"github.com/Bananenpro/cli"
910
"github.com/code-game-project/go-utils/config"
@@ -36,6 +37,25 @@ var sessionImportCmd = &cobra.Command{
3637
resp, err := http.Get(fmt.Sprintf(baseURL+"/%s?type=session", id))
3738
abortf(fmt.Sprintf("Failed to contact %s: %s", conf.ShareURL, "%s"), err)
3839

40+
for resp.StatusCode == http.StatusForbidden {
41+
password, err := cli.Input("Password:")
42+
abort(err)
43+
request, err := http.NewRequest("GET", fmt.Sprintf(baseURL+"/%s?type=session", id), nil)
44+
abort(err)
45+
request.Header.Set("Password", password)
46+
resp, err = http.DefaultClient.Do(request)
47+
abortf(fmt.Sprintf("Failed to contact %s: %s", conf.ShareURL, "%s"), err)
48+
if resp.StatusCode == http.StatusTooManyRequests {
49+
time.Sleep(1 * time.Second)
50+
resp, err = http.DefaultClient.Do(request)
51+
abortf(fmt.Sprintf("Failed to contact %s: %s", conf.ShareURL, "%s"), err)
52+
}
53+
if resp.StatusCode != http.StatusForbidden {
54+
break
55+
}
56+
cli.Error("Wrong password. Try again.")
57+
}
58+
3959
type resSession struct {
4060
GameId string `json:"game_id"`
4161
PlayerId string `json:"player_id"`

cmd/share_game.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ var shareGameCmd = &cobra.Command{
111111
}
112112
var res response
113113
err = json.NewDecoder(resp.Body).Decode(&res)
114-
cli.Error(res.Error)
114+
if err != nil {
115+
cli.Error("Failed to decode error message: %s", err)
116+
} else {
117+
cli.Error(res.Error)
118+
}
115119
return
116120
}
117121

cmd/share_session.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ var shareSessionCmd = &cobra.Command{
2020
session, err := selectSession(args)
2121
abortf("Failed to load session: %s", err)
2222

23+
password, err := cli.InputOptional("Password (optional):")
24+
abort(err)
25+
2326
type reqSession struct {
2427
GameId string `json:"game_id"`
2528
PlayerId string `json:"player_id"`
@@ -30,6 +33,7 @@ var shareSessionCmd = &cobra.Command{
3033
GameURL string `json:"game_url"`
3134
Username string `json:"username"`
3235
Session reqSession `json:"session"`
36+
Password string `json:"password"`
3337
}
3438

3539
data := request{
@@ -40,6 +44,7 @@ var shareSessionCmd = &cobra.Command{
4044
PlayerId: session.PlayerId,
4145
PlayerSecret: session.PlayerSecret,
4246
},
47+
Password: password,
4348
}
4449

4550
jsonData, err := json.Marshal(data)
@@ -60,7 +65,11 @@ var shareSessionCmd = &cobra.Command{
6065
}
6166
var res response
6267
err = json.NewDecoder(resp.Body).Decode(&res)
63-
cli.Error(res.Error)
68+
if err != nil {
69+
cli.Error("Failed to decode error message: %s", err)
70+
} else {
71+
cli.Error(res.Error)
72+
}
6473
return
6574
}
6675

cmd/share_spectate.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ var shareSpectateCmd = &cobra.Command{
8080
}
8181
var res response
8282
err = json.NewDecoder(resp.Body).Decode(&res)
83-
cli.Error(res.Error)
83+
if err != nil {
84+
cli.Error("Failed to decode error message: %s", err)
85+
} else {
86+
cli.Error(res.Error)
87+
}
8488
return
8589
}
8690

0 commit comments

Comments
 (0)