Now we can get and store the tokens, then load it when start next time.

This commit is contained in:
Tnze
2019-08-29 23:00:01 +08:00
parent 323dc15eec
commit 6a8818910c
3 changed files with 18 additions and 10 deletions

View File

@ -6,7 +6,6 @@ import (
type Access struct {
ar authResp
ct string
}
// agent is a struct of auth
@ -20,7 +19,8 @@ type proof struct {
Password string `json:"password"`
}
type tokens struct {
// Tokens store AccessToken and ClientToken
type Tokens struct {
AccessToken string `json:"accessToken"`
ClientToken string `json:"clientToken"`
}
@ -34,17 +34,18 @@ var defaultAgent = agent{
type authPayload struct {
Agent agent `json:"agent"`
proof
ClientToken string `json:"clientToken"`
ClientToken string `json:"clientToken,omitempty"`
RequestUser bool `json:"requestUser"`
}
// authResp is the response from Mojang's auth server
type authResp struct {
tokens
Tokens
AvailableProfiles []Profile `json:"availableProfiles"` // only present if the agent field was received
SelectedProfile Profile `json:"selectedProfile"` // only present if the agent field was received
User struct { // only present if requestUser was true in the request authPayload
User struct {
// only present if requestUser was true in the request authPayload
ID string `json:"id"` // hexadecimal
Properties []struct {
Name string `json:"name"`
@ -86,7 +87,7 @@ func Authenticate(user, password string) (*Access, error) {
return nil, *ar.Error
}
return &Access{ar: ar, ct: pl.ClientToken}, nil
return &Access{ar}, nil
}
func (a *Access) SelectedProfile() (ID, Name string) {
@ -100,3 +101,10 @@ func (a *Access) AccessToken() string {
func (a *Access) AvailableProfiles() []Profile {
return a.ar.AvailableProfiles
}
func (a *Access) SetTokens(tokens Tokens) {
a.ar.Tokens = tokens
}
func (a *Access) GetTokens() Tokens {
return a.ar.Tokens
}

View File

@ -3,7 +3,7 @@ package yggdrasil
import "fmt"
type refreshPayload struct {
tokens
Tokens
SelectedProfile *Profile `json:"selectedProfile,omitempty"`
RequestUser bool `json:"requestUser"`
@ -16,7 +16,7 @@ type refreshPayload struct {
// the user's password in a file
func (a *Access) Refresh(profile *Profile) error {
pl := refreshPayload{
tokens: a.ar.tokens,
Tokens: a.ar.Tokens,
SelectedProfile: profile, //used to change profile, don't use now
RequestUser: true,
}

View File

@ -7,7 +7,7 @@ import (
// Validate checks if an accessToken is usable for authentication with a Minecraft server.
func (a *Access) Validate() (bool, error) {
pl := a.ar.tokens
pl := a.ar.Tokens
resp, err := rawPost("/validate", pl)
if err != nil {
@ -19,7 +19,7 @@ func (a *Access) Validate() (bool, error) {
// Invalidate invalidates accessTokens using a client/access token pair.
func (a *Access) Invalidate() error {
pl := a.ar.tokens
pl := a.ar.Tokens
resp, err := rawPost("/invalidate", pl)
if err != nil {