Now we can get and store the tokens, then load it when start next time.
This commit is contained in:
@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
type Access struct {
|
type Access struct {
|
||||||
ar authResp
|
ar authResp
|
||||||
ct string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// agent is a struct of auth
|
// agent is a struct of auth
|
||||||
@ -20,7 +19,8 @@ type proof struct {
|
|||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type tokens struct {
|
// Tokens store AccessToken and ClientToken
|
||||||
|
type Tokens struct {
|
||||||
AccessToken string `json:"accessToken"`
|
AccessToken string `json:"accessToken"`
|
||||||
ClientToken string `json:"clientToken"`
|
ClientToken string `json:"clientToken"`
|
||||||
}
|
}
|
||||||
@ -34,17 +34,18 @@ var defaultAgent = agent{
|
|||||||
type authPayload struct {
|
type authPayload struct {
|
||||||
Agent agent `json:"agent"`
|
Agent agent `json:"agent"`
|
||||||
proof
|
proof
|
||||||
ClientToken string `json:"clientToken"`
|
ClientToken string `json:"clientToken,omitempty"`
|
||||||
RequestUser bool `json:"requestUser"`
|
RequestUser bool `json:"requestUser"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// authResp is the response from Mojang's auth server
|
// authResp is the response from Mojang's auth server
|
||||||
type authResp struct {
|
type authResp struct {
|
||||||
tokens
|
Tokens
|
||||||
AvailableProfiles []Profile `json:"availableProfiles"` // only present if the agent field was received
|
AvailableProfiles []Profile `json:"availableProfiles"` // only present if the agent field was received
|
||||||
|
|
||||||
SelectedProfile Profile `json:"selectedProfile"` // 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
|
ID string `json:"id"` // hexadecimal
|
||||||
Properties []struct {
|
Properties []struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@ -86,7 +87,7 @@ func Authenticate(user, password string) (*Access, error) {
|
|||||||
return nil, *ar.Error
|
return nil, *ar.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Access{ar: ar, ct: pl.ClientToken}, nil
|
return &Access{ar}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Access) SelectedProfile() (ID, Name string) {
|
func (a *Access) SelectedProfile() (ID, Name string) {
|
||||||
@ -100,3 +101,10 @@ func (a *Access) AccessToken() string {
|
|||||||
func (a *Access) AvailableProfiles() []Profile {
|
func (a *Access) AvailableProfiles() []Profile {
|
||||||
return a.ar.AvailableProfiles
|
return a.ar.AvailableProfiles
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Access) SetTokens(tokens Tokens) {
|
||||||
|
a.ar.Tokens = tokens
|
||||||
|
}
|
||||||
|
func (a *Access) GetTokens() Tokens {
|
||||||
|
return a.ar.Tokens
|
||||||
|
}
|
||||||
|
@ -3,7 +3,7 @@ package yggdrasil
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
type refreshPayload struct {
|
type refreshPayload struct {
|
||||||
tokens
|
Tokens
|
||||||
SelectedProfile *Profile `json:"selectedProfile,omitempty"`
|
SelectedProfile *Profile `json:"selectedProfile,omitempty"`
|
||||||
|
|
||||||
RequestUser bool `json:"requestUser"`
|
RequestUser bool `json:"requestUser"`
|
||||||
@ -16,7 +16,7 @@ type refreshPayload struct {
|
|||||||
// the user's password in a file
|
// the user's password in a file
|
||||||
func (a *Access) Refresh(profile *Profile) error {
|
func (a *Access) Refresh(profile *Profile) error {
|
||||||
pl := refreshPayload{
|
pl := refreshPayload{
|
||||||
tokens: a.ar.tokens,
|
Tokens: a.ar.Tokens,
|
||||||
SelectedProfile: profile, //used to change profile, don't use now
|
SelectedProfile: profile, //used to change profile, don't use now
|
||||||
RequestUser: true,
|
RequestUser: true,
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
// Validate checks if an accessToken is usable for authentication with a Minecraft server.
|
// Validate checks if an accessToken is usable for authentication with a Minecraft server.
|
||||||
func (a *Access) Validate() (bool, error) {
|
func (a *Access) Validate() (bool, error) {
|
||||||
pl := a.ar.tokens
|
pl := a.ar.Tokens
|
||||||
|
|
||||||
resp, err := rawPost("/validate", pl)
|
resp, err := rawPost("/validate", pl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -19,7 +19,7 @@ func (a *Access) Validate() (bool, error) {
|
|||||||
|
|
||||||
// Invalidate invalidates accessTokens using a client/access token pair.
|
// Invalidate invalidates accessTokens using a client/access token pair.
|
||||||
func (a *Access) Invalidate() error {
|
func (a *Access) Invalidate() error {
|
||||||
pl := a.ar.tokens
|
pl := a.ar.Tokens
|
||||||
|
|
||||||
resp, err := rawPost("/invalidate", pl)
|
resp, err := rawPost("/invalidate", pl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Reference in New Issue
Block a user