Compare commits
No commits in common. "045fbfead8b8c1fb06bb21ff3626fb4359801b66" and "2569e2d18d3337a27b9a9b971e626267cb2b1dc7" have entirely different histories.
045fbfead8
...
2569e2d18d
88
auth.go
88
auth.go
|
|
@ -4,13 +4,10 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"linkheader"
|
"linkheader"
|
||||||
|
|
||||||
|
|
@ -18,10 +15,6 @@ import (
|
||||||
"willnorris.com/go/microformats"
|
"willnorris.com/go/microformats"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AuthResponse struct {
|
|
||||||
Me string `json:"me"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Endpoints struct {
|
type Endpoints struct {
|
||||||
AuthorizationEndpoint string `json:"authorization_endpoint"`
|
AuthorizationEndpoint string `json:"authorization_endpoint"`
|
||||||
TokenEndpoint string `json:"token_endpoint"`
|
TokenEndpoint string `json:"token_endpoint"`
|
||||||
|
|
@ -190,84 +183,3 @@ func CreateAuthorizationURL(authURL url.URL, meURL, clientID, redirectURI, state
|
||||||
authURL.RawQuery = q.Encode()
|
authURL.RawQuery = q.Encode()
|
||||||
return authURL.String()
|
return authURL.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func VerifyAuthCode(clientID, code, redirectURI, authEndpoint string) (bool, *AuthResponse, error) {
|
|
||||||
reqData := url.Values{}
|
|
||||||
reqData.Set("code", code)
|
|
||||||
reqData.Set("client_id", clientID)
|
|
||||||
reqData.Set("redirect_uri", redirectURI)
|
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, authEndpoint, strings.NewReader(reqData.Encode()))
|
|
||||||
if err != nil {
|
|
||||||
return false, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Add("Accept", "application/json")
|
|
||||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
|
|
||||||
client := http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return false, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode == 200 {
|
|
||||||
input := io.TeeReader(resp.Body, os.Stderr)
|
|
||||||
dec := json.NewDecoder(input)
|
|
||||||
var authResponse AuthResponse
|
|
||||||
err = dec.Decode(&authResponse)
|
|
||||||
if err != nil {
|
|
||||||
return false, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return true, &authResponse, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, nil, fmt.Errorf("HTTP response code from authorization_endpoint (%s) %d", authEndpoint, resp.StatusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
type App struct {
|
|
||||||
Name string
|
|
||||||
IconURL string
|
|
||||||
}
|
|
||||||
|
|
||||||
func getPropString(mf *microformats.Microformat, prop string) string {
|
|
||||||
if v, e := mf.Properties[prop]; e {
|
|
||||||
if len(v) > 0 {
|
|
||||||
if val, ok := v[0].(string); ok {
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAppInfo(clientID string) (App, error) {
|
|
||||||
var app App
|
|
||||||
clientURL, err := url.Parse(clientID)
|
|
||||||
if err != nil {
|
|
||||||
return app, err
|
|
||||||
}
|
|
||||||
resp, err := http.Get(clientID)
|
|
||||||
if err != nil {
|
|
||||||
return app, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
md := microformats.Parse(resp.Body, clientURL)
|
|
||||||
|
|
||||||
if len(md.Items) > 0 {
|
|
||||||
mf := md.Items[0]
|
|
||||||
|
|
||||||
if mf.Type[0] == "h-x-app" || mf.Type[0] == "h-app" {
|
|
||||||
app.Name = getPropString(mf, "name")
|
|
||||||
app.IconURL = getPropString(mf, "logo")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return app, nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user