Compare commits
10 Commits
e782709911
...
93faba100e
| Author | SHA1 | Date | |
|---|---|---|---|
| 93faba100e | |||
| 5b30d00838 | |||
| 7b2a591902 | |||
| 379be5436d | |||
| ae0d3d5388 | |||
| 1afb20dd1c | |||
| e2d7962730 | |||
| a39e4b850b | |||
| 1b5c029c7b | |||
| 8083f50f6a |
|
|
@ -360,7 +360,7 @@ func (b *memoryBackend) ProcessContent(channel, fetchURL, contentType string, bo
|
||||||
|
|
||||||
err = b.updateChannelUnreadCount(conn, channel)
|
err = b.updateChannelUnreadCount(conn, channel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error: while updating channel unread count for %s: %s\n", channel, err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -373,8 +373,6 @@ func (b *memoryBackend) Fetch3(channel, fetchURL string) (*http.Response, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *memoryBackend) channelAddItem(conn redis.Conn, channel string, item microsub.Item) error {
|
func (b *memoryBackend) channelAddItem(conn redis.Conn, channel string, item microsub.Item) error {
|
||||||
// send to redis
|
|
||||||
channelKey := fmt.Sprintf("channel:%s:posts", channel)
|
|
||||||
zchannelKey := fmt.Sprintf("zchannel:%s:posts", channel)
|
zchannelKey := fmt.Sprintf("zchannel:%s:posts", channel)
|
||||||
|
|
||||||
if item.Published == "" {
|
if item.Published == "" {
|
||||||
|
|
@ -394,13 +392,10 @@ func (b *memoryBackend) channelAddItem(conn redis.Conn, channel string, item mic
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Adding item to channel %s => %s\n", channel, string(forRedis.Data))
|
|
||||||
|
|
||||||
itemKey := fmt.Sprintf("item:%s", item.ID)
|
itemKey := fmt.Sprintf("item:%s", item.ID)
|
||||||
_, err = redis.String(conn.Do("HMSET", redis.Args{}.Add(itemKey).AddFlat(&forRedis)...))
|
_, err = redis.String(conn.Do("HMSET", redis.Args{}.Add(itemKey).AddFlat(&forRedis)...))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error while writing item for redis: %v\n", err)
|
return fmt.Errorf("error while writing item for redis: %v", err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
readChannelKey := fmt.Sprintf("channel:%s:read", channel)
|
readChannelKey := fmt.Sprintf("channel:%s:read", channel)
|
||||||
|
|
@ -413,27 +408,14 @@ func (b *memoryBackend) channelAddItem(conn redis.Conn, channel string, item mic
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
added, err := redis.Int64(conn.Do("SADD", channelKey, itemKey))
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("error while adding item %s to channel %s for redis: %v\n", itemKey, channelKey, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
score, err := time.Parse(time.RFC3339, item.Published)
|
score, err := time.Parse(time.RFC3339, item.Published)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error can't parse %s as time\n", item.Published)
|
return fmt.Errorf("error can't parse %s as time", item.Published)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
added, err = redis.Int64(conn.Do("ZADD", zchannelKey, score.Unix()*1.0, itemKey))
|
_, err = redis.Int64(conn.Do("ZADD", zchannelKey, score.Unix()*1.0, itemKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error while zadding item %s to channel %s for redis: %v\n", itemKey, channelKey, err)
|
return fmt.Errorf("error while zadding item %s to channel %s for redis: %v", itemKey, zchannelKey, err)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if added > 0 {
|
|
||||||
log.Printf("Added item to channel %s\n", channel)
|
|
||||||
log.Println(item)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -444,8 +426,9 @@ func (b *memoryBackend) updateChannelUnreadCount(conn redis.Conn, channel string
|
||||||
zchannelKey := fmt.Sprintf("zchannel:%s:posts", channel)
|
zchannelKey := fmt.Sprintf("zchannel:%s:posts", channel)
|
||||||
unread, err := redis.Int(conn.Do("ZCARD", zchannelKey))
|
unread, err := redis.Int(conn.Do("ZCARD", zchannelKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("error: while updating channel unread count for %s: %s", channel, err)
|
||||||
}
|
}
|
||||||
|
defer b.save()
|
||||||
c.Unread = unread
|
c.Unread = unread
|
||||||
b.Channels[channel] = c
|
b.Channels[channel] = c
|
||||||
}
|
}
|
||||||
|
|
@ -465,7 +448,7 @@ func Fetch2(fetchURL string) (*http.Response, error) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
if !strings.HasPrefix(fetchURL, "http") {
|
if !strings.HasPrefix(fetchURL, "http") {
|
||||||
return nil, fmt.Errorf("error parsing %s as url", fetchURL)
|
return nil, fmt.Errorf("error parsing %s as url, has no http(s) prefix", fetchURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
u, err := url.Parse(fetchURL)
|
u, err := url.Parse(fetchURL)
|
||||||
|
|
|
||||||
|
|
@ -18,16 +18,20 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/garyburd/redigo/redis"
|
"github.com/garyburd/redigo/redis"
|
||||||
|
"github.com/pstuifzand/ekster/pkg/indieauth"
|
||||||
"github.com/pstuifzand/ekster/pkg/microsub"
|
"github.com/pstuifzand/ekster/pkg/microsub"
|
||||||
|
"github.com/pstuifzand/ekster/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -49,12 +53,194 @@ type mainHandler struct {
|
||||||
Backend *memoryBackend
|
Backend *memoryBackend
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type session struct {
|
||||||
|
AuthorizationEndpoint string `redis:"authorization_endpoint"`
|
||||||
|
Me string `redis:"me"`
|
||||||
|
RedirectURI string `redis:"redirect_uri"`
|
||||||
|
State string `redis:"state"`
|
||||||
|
ClientID string `redis:"client_id"`
|
||||||
|
LoggedIn bool `redis:"logged_in"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type authResponse struct {
|
||||||
|
Me string `json:"me"`
|
||||||
|
}
|
||||||
|
|
||||||
func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == http.MethodGet {
|
conn := pool.Get()
|
||||||
fmt.Fprintln(w, "<h1>Ekster - Microsub server</h1>")
|
defer conn.Close()
|
||||||
fmt.Fprintln(w, `<p><a href="/settings">Settings</a></p>`)
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, fmt.Sprintf("Bad Request: %s", err.Error()), 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if r.Method == http.MethodGet {
|
||||||
|
if r.URL.Path == "/" {
|
||||||
|
c, err := r.Cookie("session")
|
||||||
|
sessionVar := util.RandStringBytes(16)
|
||||||
|
|
||||||
|
if err == http.ErrNoCookie {
|
||||||
|
newCookie := &http.Cookie{
|
||||||
|
Name: "session",
|
||||||
|
Value: sessionVar,
|
||||||
|
Expires: time.Now().Add(24 * time.Hour),
|
||||||
|
}
|
||||||
|
|
||||||
|
http.SetCookie(w, newCookie)
|
||||||
|
} else {
|
||||||
|
sessionVar = c.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
var sess session
|
||||||
|
sessionKey := "session:" + sessionVar
|
||||||
|
data, err := redis.Values(conn.Do("HGETALL", sessionKey))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(w, "ERROR: %q\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = redis.ScanStruct(data, &sess)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(w, "ERROR: %q\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintln(w, "<h1>Ekster - Microsub server</h1>")
|
||||||
|
fmt.Fprintln(w, `<p><a href="/settings">Settings</a></p>`)
|
||||||
|
|
||||||
|
if sess.LoggedIn {
|
||||||
|
fmt.Fprintf(w, "SUCCESS Me = %s", sess.Me)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintln(w, `
|
||||||
|
<h2>Sign in to Ekster</h2>
|
||||||
|
<form action="/auth" method="post">
|
||||||
|
<input type="text" name="url" placeholder="https://example.com/">
|
||||||
|
<button type="submit">Login</button>
|
||||||
|
</form>
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
} else if r.URL.Path == "/auth/callback" {
|
||||||
|
c, err := r.Cookie("session")
|
||||||
|
if err == http.ErrNoCookie {
|
||||||
|
http.Redirect(w, r, "/", 302)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sessionVar := c.Value
|
||||||
|
var sess session
|
||||||
|
sessionKey := "session:" + sessionVar
|
||||||
|
data, err := redis.Values(conn.Do("HGETALL", sessionKey))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(w, "ERROR: %q\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = redis.ScanStruct(data, &sess)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(w, "ERROR: %q\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
state := r.Form.Get("state")
|
||||||
|
if state != sess.State {
|
||||||
|
fmt.Fprintf(w, "ERROR: Mismatched state\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
code := r.Form.Get("code")
|
||||||
|
|
||||||
|
reqData := url.Values{}
|
||||||
|
reqData.Set("code", code)
|
||||||
|
reqData.Set("client_id", sess.ClientID)
|
||||||
|
reqData.Set("redirect_uri", sess.RedirectURI)
|
||||||
|
resp, err := http.PostForm(sess.AuthorizationEndpoint, reqData)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(w, "ERROR: %q\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode == 200 {
|
||||||
|
dec := json.NewDecoder(resp.Body)
|
||||||
|
var authResponse authResponse
|
||||||
|
err = dec.Decode(&authResponse)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(w, "ERROR: %q\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println(authResponse)
|
||||||
|
|
||||||
|
sess.Me = authResponse.Me
|
||||||
|
sess.LoggedIn = true
|
||||||
|
conn.Do("HMSET", redis.Args{}.Add(sessionKey).AddFlat(sess)...)
|
||||||
|
http.Redirect(w, r, "/", 302)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(w, "ERROR: HTTP response code from authorization_endpoint (%s) %d \n", sess.AuthorizationEndpoint, resp.StatusCode)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if r.Method == http.MethodPost {
|
||||||
|
if r.URL.Path == "/auth" {
|
||||||
|
c, err := r.Cookie("session")
|
||||||
|
if err == http.ErrNoCookie {
|
||||||
|
http.Redirect(w, r, "/", 302)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionVar := c.Value
|
||||||
|
|
||||||
|
// redirect to endpoint
|
||||||
|
me := r.Form.Get("url")
|
||||||
|
log.Println(me)
|
||||||
|
meURL, err := url.Parse(me)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Bad Request: %s, %s", err.Error(), me), 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
endpoints, err := indieauth.GetEndpoints(meURL)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Bad Request: %s %s", err.Error(), me), 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println(endpoints)
|
||||||
|
|
||||||
|
authURL, err := url.Parse(endpoints.AuthorizationEndpoint)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Bad Request: %s %s", err.Error(), me), 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println(authURL)
|
||||||
|
|
||||||
|
state := util.RandStringBytes(16)
|
||||||
|
clientID := "https://p83.nl/microsub-client"
|
||||||
|
redirectURI := fmt.Sprintf("%s/auth/callback", os.Getenv("EKSTER_BASEURL"))
|
||||||
|
|
||||||
|
sess := session{
|
||||||
|
AuthorizationEndpoint: endpoints.AuthorizationEndpoint,
|
||||||
|
Me: meURL.String(),
|
||||||
|
State: state,
|
||||||
|
RedirectURI: redirectURI,
|
||||||
|
ClientID: clientID,
|
||||||
|
LoggedIn: false,
|
||||||
|
}
|
||||||
|
conn.Do("HMSET", redis.Args{}.Add("session:"+sessionVar).AddFlat(&sess)...)
|
||||||
|
|
||||||
|
q := authURL.Query()
|
||||||
|
q.Add("response_type", "id")
|
||||||
|
q.Add("me", meURL.String())
|
||||||
|
q.Add("client_id", clientID)
|
||||||
|
q.Add("redirect_uri", redirectURI)
|
||||||
|
q.Add("state", state)
|
||||||
|
authURL.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
http.Redirect(w, r, authURL.String(), 302)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -120,6 +306,8 @@ func main() {
|
||||||
})
|
})
|
||||||
|
|
||||||
backend.(*memoryBackend).run()
|
backend.(*memoryBackend).run()
|
||||||
|
hubBackend.run()
|
||||||
|
|
||||||
log.Printf("Listening on port %d\n", port)
|
log.Printf("Listening on port %d\n", port)
|
||||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,10 +24,6 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
log.Printf("%s %s\n", r.Method, r.URL)
|
|
||||||
log.Println(r.URL.Query())
|
|
||||||
log.Println(r.PostForm)
|
|
||||||
log.Println(r.Header)
|
|
||||||
|
|
||||||
if r.Method == http.MethodPost {
|
if r.Method == http.MethodPost {
|
||||||
sourceID := r.URL.Query().Get("source_id")
|
sourceID := r.URL.Query().Get("source_id")
|
||||||
|
|
@ -74,7 +70,10 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
id, _ := redis.Int(conn.Do("INCR", "source:"+sourceID+"next_id"))
|
id, _ := redis.Int(conn.Do("INCR", "source:"+sourceID+"next_id"))
|
||||||
item.ID = fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprintf("source:%s:%d", sourceID, id))))
|
item.ID = fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprintf("source:%s:%d", sourceID, id))))
|
||||||
h.Backend.channelAddItem(conn, channel, item)
|
h.Backend.channelAddItem(conn, channel, item)
|
||||||
h.Backend.updateChannelUnreadCount(conn, channel)
|
err = h.Backend.updateChannelUnreadCount(conn, channel)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error: while updating channel unread count for %s: %s\n", channel, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ func Authorize(me *url.URL, endpoints Endpoints, clientID, scope string) (TokenR
|
||||||
if state != responseState {
|
if state != responseState {
|
||||||
log.Println("Wrong state response")
|
log.Println("Wrong state response")
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, `<div style="width:100%%;height:100%%;display: flex; align-items: center; justify-content: center;">You can close this window, proceed on the command line</div>`)
|
fmt.Fprintln(w, `<div style="width:100%;height:100%;display: flex; align-items: center; justify-content: center;">You can close this window, proceed on the command line</div>`)
|
||||||
close(shutdown)
|
close(shutdown)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user