-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
session system (preparing for premium resources)
- Loading branch information
1 parent
7fb5c60
commit a3bd07b
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |