Add more code to indieauth

master
Peter Stuifzand 6 years ago
parent 2569e2d18d
commit c9290a051d

@ -4,10 +4,13 @@ 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"
@ -15,6 +18,10 @@ 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"`
@ -183,3 +190,86 @@ 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(code, redirectURI, authEndpoint string) (bool, *AuthResponse, error) {
const ClientID = "https://p83.nl/track-me"
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…
Cancel
Save