1package ooapi
2
3import (
4	"crypto/rand"
5	"encoding/base64"
6	"time"
7
8	"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
9	"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
10)
11
12// loginState is the struct saved in the kvstore
13// to keep track of the login state.
14type loginState struct {
15	ClientID string
16	Expire   time.Time
17	Password string
18	Token    string
19}
20
21func (ls *loginState) credentialsValid() bool {
22	return ls.ClientID != "" && ls.Password != ""
23}
24
25func (ls *loginState) tokenValid() bool {
26	return ls.Token != "" && time.Now().Add(60*time.Second).Before(ls.Expire)
27}
28
29// loginKey is the key with which loginState is saved
30// into the key-value store used by Client.
31const loginKey = "orchestra.state"
32
33// newRandomPassword generates a new random password.
34func newRandomPassword() string {
35	b := make([]byte, 48)
36	_, err := rand.Read(b)
37	runtimex.PanicOnError(err, "rand.Read failed")
38	return base64.StdEncoding.EncodeToString(b)
39}
40
41// newRegisterRequest creates a new RegisterRequest.
42func newRegisterRequest(password string) *apimodel.RegisterRequest {
43	return &apimodel.RegisterRequest{
44		// The original implementation has as its only use case that we
45		// were registering and logging in for sending an update regarding
46		// the probe whereabouts. Yet here in probe-engine, the orchestra
47		// is currently only used to fetch inputs. For this purpose, we don't
48		// need to communicate any specific information. The code that will
49		// perform an update used to be responsible of doing that. Now, we
50		// are not using orchestra for this purpose anymore.
51		Platform:        "miniooni",
52		ProbeASN:        "AS0",
53		ProbeCC:         "ZZ",
54		SoftwareName:    "miniooni",
55		SoftwareVersion: "0.1.0-dev",
56		SupportedTests:  []string{"web_connectivity"},
57		Password:        password,
58	}
59}
60