260705_012936
This commit is contained in:
parent
faf42566f5
commit
f45f164211
43
moira_db.go
43
moira_db.go
@ -31,6 +31,19 @@ import (
|
|||||||
var templates *template.Template = template.New("")
|
var templates *template.Template = template.New("")
|
||||||
var client = &http.Client{Timeout: 2*time.Second}
|
var client = &http.Client{Timeout: 2*time.Second}
|
||||||
|
|
||||||
|
|
||||||
|
type UserInfo struct{
|
||||||
|
ProviderCode, // DC--Discord, GG--Google, GH--GitHub, LC--local database
|
||||||
|
ExtID,
|
||||||
|
Username,
|
||||||
|
Fullname,
|
||||||
|
Email,
|
||||||
|
Locale string
|
||||||
|
|
||||||
|
CreatedTS,
|
||||||
|
UpdatedTS int64 // UNIX timestamps
|
||||||
|
}
|
||||||
|
|
||||||
var oauth2_dc_config = &oauth2.Config{
|
var oauth2_dc_config = &oauth2.Config{
|
||||||
ClientID: os.Getenv("MOIRA_OAUTH2_DC_CLI_ID"),
|
ClientID: os.Getenv("MOIRA_OAUTH2_DC_CLI_ID"),
|
||||||
ClientSecret: os.Getenv("MOIRA_OAUTH2_DC_CLI_SECRET"),
|
ClientSecret: os.Getenv("MOIRA_OAUTH2_DC_CLI_SECRET"),
|
||||||
@ -82,7 +95,7 @@ func oauth2_dc_exchangeReceivedCode(req *http.Request) (_ *http.Client, err erro
|
|||||||
return oauth2_dc_config.Client(context.Background(), token), nil
|
return oauth2_dc_config.Client(context.Background(), token), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func oauth2_fetch_dc_userInfo(in_client *http.Client) (ret string, err error){
|
func oauth2_fetch_dcUserInfo(in_client *http.Client) (ret []byte, err error){
|
||||||
req, err := http.NewRequest("GET", "https://discord.com/api/users/@me", nil)
|
req, err := http.NewRequest("GET", "https://discord.com/api/users/@me", nil)
|
||||||
if err != nil{ return }
|
if err != nil{ return }
|
||||||
|
|
||||||
@ -96,13 +109,26 @@ func oauth2_fetch_dc_userInfo(in_client *http.Client) (ret string, err error){
|
|||||||
if err != nil{ return }
|
if err != nil{ return }
|
||||||
//fmt.Printf("%+v\n", string(resp_text))
|
//fmt.Printf("%+v\n", string(resp_text))
|
||||||
|
|
||||||
return string(resp_text), nil
|
return resp_text, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func oauth2_dc_userInfo_to_User(resp_text string) (err error){ // TODO: add User{} return
|
func oauth2_dcUserInfo_to_UserInfo(resp_text []byte) (ret UserInfo, err error){
|
||||||
fmt.Printf("%s\n", resp_text)
|
//fmt.Printf("%s\n", string(resp_text))
|
||||||
|
// TODO: you might want to check if the data fields are not empty
|
||||||
|
|
||||||
return nil
|
ts_now := time.Now().Unix()
|
||||||
|
|
||||||
|
return UserInfo{
|
||||||
|
ProviderCode: "DC",
|
||||||
|
ExtID: gjson.GetBytes(resp_text, "id").String(),
|
||||||
|
Username: gjson.GetBytes(resp_text, "username").String(),
|
||||||
|
Fullname: gjson.GetBytes(resp_text, "global_name").String(),
|
||||||
|
Email: gjson.GetBytes(resp_text, "email").String(),
|
||||||
|
Locale: gjson.GetBytes(resp_text, "locale").String(),
|
||||||
|
|
||||||
|
CreatedTS: ts_now,
|
||||||
|
UpdatedTS: ts_now,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func oauth2_hdlr_login(resp http.ResponseWriter, req *http.Request){
|
func oauth2_hdlr_login(resp http.ResponseWriter, req *http.Request){
|
||||||
@ -119,11 +145,14 @@ func oauth2_hdlr_clbk(resp http.ResponseWriter, req *http.Request){
|
|||||||
client, err := oauth2_dc_exchangeReceivedCode(req)
|
client, err := oauth2_dc_exchangeReceivedCode(req)
|
||||||
if err != nil { return } // TODO: should the error be reported somehow?
|
if err != nil { return } // TODO: should the error be reported somehow?
|
||||||
|
|
||||||
resp_text, err := oauth2_fetch_dc_userInfo(client)
|
resp_text, err := oauth2_fetch_dcUserInfo(client)
|
||||||
if err != nil { return } // TODO: should the error be reported somehow?
|
if err != nil { return } // TODO: should the error be reported somehow?
|
||||||
|
|
||||||
err = oauth2_dc_userInfo_to_User(resp_text)
|
new_userInfo, err := oauth2_dcUserInfo_to_UserInfo(resp_text)
|
||||||
if err != nil { return } // TODO: should the error be reported somehow?
|
if err != nil { return } // TODO: should the error be reported somehow?
|
||||||
|
|
||||||
|
t_json, _ := json.MarshalIndent(new_userInfo, "", " ")
|
||||||
|
fmt.Printf("%+v\n", string(t_json))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user