2021-03-04 11:51:07 +01:00
|
|
|
// Code generated by go generate; DO NOT EDIT.
|
2021-09-05 14:49:38 +02:00
|
|
|
// 2021-09-05 13:54:21.648163 +0200 CEST m=+0.000177959
|
2021-03-04 11:51:07 +01:00
|
|
|
|
|
|
|
package ooapi
|
|
|
|
|
|
|
|
//go:generate go run ./internal/generator -file login.go
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel"
|
2021-03-04 11:51:07 +01:00
|
|
|
)
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
// withLoginPsiphonConfigAPI implements login for simplePsiphonConfigAPI.
|
|
|
|
type withLoginPsiphonConfigAPI struct {
|
|
|
|
API clonerForPsiphonConfigAPI // mandatory
|
|
|
|
JSONCodec JSONCodec // optional
|
|
|
|
KVStore KVStore // mandatory
|
|
|
|
RegisterAPI callerForRegisterAPI // mandatory
|
|
|
|
LoginAPI callerForLoginAPI // mandatory
|
2021-03-04 11:51:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call logins, if needed, then calls the API.
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginPsiphonConfigAPI) Call(ctx context.Context, req *apimodel.PsiphonConfigRequest) (apimodel.PsiphonConfigResponse, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
token, err := api.maybeLogin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp, err := api.API.WithToken(token).Call(ctx, req)
|
|
|
|
if errors.Is(err, ErrUnauthorized) {
|
|
|
|
// Maybe the clock is just off? Let's try to obtain
|
|
|
|
// a token again and see if this fixes it.
|
|
|
|
if token, err = api.forceLogin(ctx); err == nil {
|
|
|
|
switch resp, err = api.API.WithToken(token).Call(ctx, req); err {
|
|
|
|
case nil:
|
|
|
|
return resp, nil
|
|
|
|
case ErrUnauthorized:
|
|
|
|
// fallthrough
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Okay, this seems a broader problem. How about we try
|
|
|
|
// and re-register ourselves again instead?
|
|
|
|
token, err = api.forceRegister(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp, err = api.API.WithToken(token).Call(ctx, req)
|
|
|
|
// fallthrough
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginPsiphonConfigAPI) jsonCodec() JSONCodec {
|
2021-03-04 11:51:07 +01:00
|
|
|
if api.JSONCodec != nil {
|
|
|
|
return api.JSONCodec
|
|
|
|
}
|
|
|
|
return &defaultJSONCodec{}
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginPsiphonConfigAPI) readstate() (*loginState, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
data, err := api.KVStore.Get(loginKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ls loginState
|
|
|
|
if err := api.jsonCodec().Decode(data, &ls); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ls, nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginPsiphonConfigAPI) writestate(ls *loginState) error {
|
2021-03-04 11:51:07 +01:00
|
|
|
data, err := api.jsonCodec().Encode(*ls)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return api.KVStore.Set(loginKey, data)
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginPsiphonConfigAPI) doRegister(ctx context.Context, password string) (string, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
req := newRegisterRequest(password)
|
|
|
|
ls := &loginState{}
|
|
|
|
resp, err := api.RegisterAPI.Call(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ls.ClientID = resp.ClientID
|
|
|
|
ls.Password = req.Password
|
|
|
|
return api.doLogin(ctx, ls)
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginPsiphonConfigAPI) forceRegister(ctx context.Context) (string, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
var password string
|
|
|
|
// If we already have a previous password, let us keep
|
|
|
|
// using it. This will allow a new version of the API to
|
|
|
|
// be able to continue to identify this probe. (This
|
|
|
|
// assumes that we have a stateless API that generates
|
|
|
|
// the user ID as a signature of the password plus a
|
|
|
|
// timestamp and that the key to generate the signature
|
|
|
|
// is not lost. If all these conditions are met, we
|
|
|
|
// can then serve better test targets to more long running
|
|
|
|
// (and therefore trusted) probes.)
|
|
|
|
if ls, err := api.readstate(); err == nil {
|
|
|
|
password = ls.Password
|
|
|
|
}
|
|
|
|
if password == "" {
|
|
|
|
password = newRandomPassword()
|
|
|
|
}
|
|
|
|
return api.doRegister(ctx, password)
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginPsiphonConfigAPI) forceLogin(ctx context.Context) (string, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
ls, err := api.readstate()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return api.doLogin(ctx, ls)
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginPsiphonConfigAPI) maybeLogin(ctx context.Context) (string, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
ls, _ := api.readstate()
|
|
|
|
if ls == nil || !ls.credentialsValid() {
|
|
|
|
return api.forceRegister(ctx)
|
|
|
|
}
|
|
|
|
if !ls.tokenValid() {
|
|
|
|
return api.doLogin(ctx, ls)
|
|
|
|
}
|
|
|
|
return ls.Token, nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginPsiphonConfigAPI) doLogin(ctx context.Context, ls *loginState) (string, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
req := &apimodel.LoginRequest{
|
|
|
|
ClientID: ls.ClientID,
|
|
|
|
Password: ls.Password,
|
|
|
|
}
|
|
|
|
resp, err := api.LoginAPI.Call(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ls.Token = resp.Token
|
|
|
|
ls.Expire = resp.Expire
|
|
|
|
if err := api.writestate(ls); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return ls.Token, nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
var _ callerForPsiphonConfigAPI = &withLoginPsiphonConfigAPI{}
|
2021-03-04 11:51:07 +01:00
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
// withLoginTorTargetsAPI implements login for simpleTorTargetsAPI.
|
|
|
|
type withLoginTorTargetsAPI struct {
|
|
|
|
API clonerForTorTargetsAPI // mandatory
|
|
|
|
JSONCodec JSONCodec // optional
|
|
|
|
KVStore KVStore // mandatory
|
|
|
|
RegisterAPI callerForRegisterAPI // mandatory
|
|
|
|
LoginAPI callerForLoginAPI // mandatory
|
2021-03-04 11:51:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call logins, if needed, then calls the API.
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginTorTargetsAPI) Call(ctx context.Context, req *apimodel.TorTargetsRequest) (apimodel.TorTargetsResponse, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
token, err := api.maybeLogin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp, err := api.API.WithToken(token).Call(ctx, req)
|
|
|
|
if errors.Is(err, ErrUnauthorized) {
|
|
|
|
// Maybe the clock is just off? Let's try to obtain
|
|
|
|
// a token again and see if this fixes it.
|
|
|
|
if token, err = api.forceLogin(ctx); err == nil {
|
|
|
|
switch resp, err = api.API.WithToken(token).Call(ctx, req); err {
|
|
|
|
case nil:
|
|
|
|
return resp, nil
|
|
|
|
case ErrUnauthorized:
|
|
|
|
// fallthrough
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Okay, this seems a broader problem. How about we try
|
|
|
|
// and re-register ourselves again instead?
|
|
|
|
token, err = api.forceRegister(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp, err = api.API.WithToken(token).Call(ctx, req)
|
|
|
|
// fallthrough
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginTorTargetsAPI) jsonCodec() JSONCodec {
|
2021-03-04 11:51:07 +01:00
|
|
|
if api.JSONCodec != nil {
|
|
|
|
return api.JSONCodec
|
|
|
|
}
|
|
|
|
return &defaultJSONCodec{}
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginTorTargetsAPI) readstate() (*loginState, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
data, err := api.KVStore.Get(loginKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ls loginState
|
|
|
|
if err := api.jsonCodec().Decode(data, &ls); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ls, nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginTorTargetsAPI) writestate(ls *loginState) error {
|
2021-03-04 11:51:07 +01:00
|
|
|
data, err := api.jsonCodec().Encode(*ls)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return api.KVStore.Set(loginKey, data)
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginTorTargetsAPI) doRegister(ctx context.Context, password string) (string, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
req := newRegisterRequest(password)
|
|
|
|
ls := &loginState{}
|
|
|
|
resp, err := api.RegisterAPI.Call(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ls.ClientID = resp.ClientID
|
|
|
|
ls.Password = req.Password
|
|
|
|
return api.doLogin(ctx, ls)
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginTorTargetsAPI) forceRegister(ctx context.Context) (string, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
var password string
|
|
|
|
// If we already have a previous password, let us keep
|
|
|
|
// using it. This will allow a new version of the API to
|
|
|
|
// be able to continue to identify this probe. (This
|
|
|
|
// assumes that we have a stateless API that generates
|
|
|
|
// the user ID as a signature of the password plus a
|
|
|
|
// timestamp and that the key to generate the signature
|
|
|
|
// is not lost. If all these conditions are met, we
|
|
|
|
// can then serve better test targets to more long running
|
|
|
|
// (and therefore trusted) probes.)
|
|
|
|
if ls, err := api.readstate(); err == nil {
|
|
|
|
password = ls.Password
|
|
|
|
}
|
|
|
|
if password == "" {
|
|
|
|
password = newRandomPassword()
|
|
|
|
}
|
|
|
|
return api.doRegister(ctx, password)
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginTorTargetsAPI) forceLogin(ctx context.Context) (string, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
ls, err := api.readstate()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return api.doLogin(ctx, ls)
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginTorTargetsAPI) maybeLogin(ctx context.Context) (string, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
ls, _ := api.readstate()
|
|
|
|
if ls == nil || !ls.credentialsValid() {
|
|
|
|
return api.forceRegister(ctx)
|
|
|
|
}
|
|
|
|
if !ls.tokenValid() {
|
|
|
|
return api.doLogin(ctx, ls)
|
|
|
|
}
|
|
|
|
return ls.Token, nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
func (api *withLoginTorTargetsAPI) doLogin(ctx context.Context, ls *loginState) (string, error) {
|
2021-03-04 11:51:07 +01:00
|
|
|
req := &apimodel.LoginRequest{
|
|
|
|
ClientID: ls.ClientID,
|
|
|
|
Password: ls.Password,
|
|
|
|
}
|
|
|
|
resp, err := api.LoginAPI.Call(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ls.Token = resp.Token
|
|
|
|
ls.Expire = resp.Expire
|
|
|
|
if err := api.writestate(ls); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return ls.Token, nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:30:42 +01:00
|
|
|
var _ callerForTorTargetsAPI = &withLoginTorTargetsAPI{}
|