Skip to content

Commit

Permalink
session system (preparing for premium resources)
Browse files Browse the repository at this point in the history
  • Loading branch information
quiquelhappy committed Sep 14, 2021
1 parent 7fb5c60 commit a3bd07b
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/elements/account/Account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package account

type Account struct {
Username string `json:"username"`
GithubId string `json:"githubId"`
Id string `json:"id"`
}
60 changes: 60 additions & 0 deletions src/elements/session/LoginRequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package session

import (
"encoding/json"
"github.com/grifpkg/cli/api"
"time"
)

type LoginRequest struct {
Id string `json:"id"`
Hash string `json:"hash"`
Creation int `json:"creation"`
Expiry int `json:"expiry"`
Session Session `json:"session"`
}

func Start() (loginRequest LoginRequest, err error){
api.LogOne(api.Progress,"starting login request")
request, err := api.Request("login/request/start/", map[string]string{}, nil)
loginRequest = LoginRequest{}
err = json.NewDecoder(request).Decode(&loginRequest)
return loginRequest, err
}

func (loginRequest *LoginRequest) Get() (err error){
api.LogOne(api.Progress,"updating login request")
request, err := api.Request("login/request/get/", map[string]string{
"request": loginRequest.Hash,
}, nil)
err = json.NewDecoder(request).Decode(&loginRequest)
return err
}

func (loginRequest *LoginRequest) Validate(githubToken string) (err error){
api.LogOne(api.Progress,"validating login request")
request, err := api.Request("login/request/validate/", map[string]string{
"request": loginRequest.Hash,
"token": githubToken,
}, nil)
err = json.NewDecoder(request).Decode(&loginRequest)
return err
}

func (loginRequest *LoginRequest) AwaitUntilValidated() (err error){
for true {
err := loginRequest.Get()
if err!=nil {
return err
} else if (loginRequest.Session != Session{}) {
return nil
}
api.LogOne(api.Progress,"awaiting for the request to be validated")
time.Sleep(2 * time.Second)
}
return nil
}

func (loginRequest LoginRequest) GetAuthURL() (url string){
return "https://api.grifpkg.com/rest/1/login/?lr="+loginRequest.Id
}
90 changes: 90 additions & 0 deletions src/elements/session/Session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package session

import (
"encoding/json"
"github.com/fatih/color"
"github.com/grifpkg/cli/api"
"github.com/zalando/go-keyring"
"log"
)

type Session struct {
Id string `json:"id"`
Hash string `json:"hash"`
UserAgent string `json:"userAgent"`
Creation int `json:"creation"`
Expiry int `json:"expiry"`
City interface{} `json:"city"`
Country interface{} `json:"country"`
}

func (session *Session) Close() (err error){
if len(session.Hash)>0 {
api.LogOne(api.Progress,"logging out")
request, err := api.Request("session/close/", map[string]string{}, GetHash())
if err!=nil {
err = nil // ignore error
} else {
err = json.NewDecoder(request).Decode(&session)
err = nil // ignore error
}
}
api.LogOne(api.Progress,"removing session from the keychain")
err = keyring.Delete(api.KeychainService, api.KeychainHash)
if err != nil {
return err
}
return nil
}

func GetHash() (hash interface{}){
currentSession, err := Get()
if err!=nil || currentSession.Hash=="" {
return nil
} else {
return currentSession.Hash
}
}

func Get() (session Session, err error){
api.LogOne(api.Progress,"loading grifpkg's session hash from the keychain")
secret, err := keyring.Get(api.KeychainService, api.KeychainHash)
if err != nil {
api.LogOne(api.Progress,"no sessions found, requesting login")
login, err := Start()
api.Log(api.Info, []api.Message{
{
Value: "please, login to your grifpkg account by using this link:",
Color: nil,
},
{
Value: login.GetAuthURL(),
Color: color.New(color.FgHiBlue),
},
})
if err!=nil {
return Session{}, err
}
err = login.AwaitUntilValidated()
if err!=nil {
return Session{}, err
}
api.LogOne(api.Progress,"saving newly generated session into the keychain")
session, err := json.Marshal(login.Session)
if err!=nil {
return Session{}, err
}
err = keyring.Set(api.KeychainService, api.KeychainHash, string(session))
if err != nil {
log.Fatal(err)
}
return login.Session, nil
} else {
finalSession := Session{}
err := json.Unmarshal([]byte(secret), &finalSession)
if err!=nil {
return Session{}, err
}
return finalSession, nil
}
}

0 comments on commit a3bd07b

Please sign in to comment.