GODT-35: New pmapi client and manager using resty

This commit is contained in:
James Houlahan
2021-02-22 18:23:51 +01:00
committed by Jakub
parent 1d538e8540
commit 2284e9ede1
163 changed files with 3333 additions and 8124 deletions

View File

@ -19,16 +19,36 @@ package fakeapi
import (
"errors"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
)
type fakeSession struct {
username string
uid, refreshToken string
hasFullScope bool
username string
uid, acc, ref string
hasFullScope bool
}
var errWrongNameOrPassword = errors.New("Incorrect login credentials. Please try again") //nolint[stylecheck]
func (ctl *Controller) checkAccessToken(uid, acc string) bool {
session, ok := ctl.sessionsByUID[uid]
if !ok {
return false
}
return session.uid == uid && session.acc == acc
}
func (ctl *Controller) checkScope(uid string) bool {
session, ok := ctl.sessionsByUID[uid]
if !ok {
return false
}
return session.hasFullScope
}
func (ctl *Controller) createSessionIfAuthorized(username, password string) (*fakeSession, error) {
// get user
user, ok := ctl.usersByUsername[username]
@ -40,16 +60,32 @@ func (ctl *Controller) createSessionIfAuthorized(username, password string) (*fa
session := &fakeSession{
username: username,
uid: ctl.tokenGenerator.next("uid"),
acc: ctl.tokenGenerator.next("acc"),
ref: ctl.tokenGenerator.next("ref"),
hasFullScope: !user.has2FA,
}
ctl.refreshTheTokensForSession(session)
ctl.sessionsByUID[session.uid] = session
return session, nil
}
func (ctl *Controller) refreshTheTokensForSession(session *fakeSession) {
session.refreshToken = ctl.tokenGenerator.next("refresh")
func (ctl *Controller) refreshSessionIfAuthorized(uid, ref string) (*fakeSession, error) {
session, ok := ctl.sessionsByUID[uid]
if !ok {
return nil, pmapi.ErrUnauthorized
}
if ref != session.ref {
return nil, pmapi.ErrUnauthorized
}
session.ref = ctl.tokenGenerator.next("ref")
session.acc = ctl.tokenGenerator.next("acc")
ctl.sessionsByUID[session.uid] = session
return session, nil
}
func (ctl *Controller) deleteSession(uid string) {