all repos — honk @ e6901e8a79670178ecc7eb4fc54a772e6c410368

my fork of honk

masto.go (view raw)

  1package main
  2
  3import (
  4	"fmt"
  5	"net/http"
  6
  7	"humungus.tedunangst.com/r/webs/junk"
  8	"humungus.tedunangst.com/r/webs/login"
  9)
 10
 11func showoauthlogin(rw http.ResponseWriter, r *http.Request) {
 12	templinfo := make(map[string]interface{})
 13	templinfo = getInfo(r)
 14	templinfo["ClientID"] = r.URL.Query().Get("client_id")
 15	templinfo["RedirectURI"] = r.URL.Query().Get("redirect_uri")
 16
 17	if err := readviews.Execute(rw, "oauthlogin.html", templinfo); err != nil {
 18		elog.Println(err)
 19	}
 20}
 21
 22// https://docs.joinmastodon.org/methods/apps/#create
 23func apiapps(rw http.ResponseWriter, r *http.Request) {
 24	if err := r.ParseForm(); err != nil {
 25		http.Error(rw, "invalid input", http.StatusUnprocessableEntity)
 26		elog.Println(err)
 27		return
 28	}
 29	clientName := r.Form.Get("client_name")
 30	redirectURI := r.Form.Get("redirect_uris")
 31	scopes := r.Form.Get("scopes")
 32	website := r.Form.Get("website")
 33	clientID := tokengen()
 34	clientSecret := tokengen()
 35	vapidKey := tokengen()
 36
 37	_, err := stmtSaveMastoApp.Exec(clientName, redirectURI, scopes, clientID, clientSecret, vapidKey, "")
 38	if err != nil {
 39		elog.Printf("error saving masto app: %v", err)
 40		http.Error(rw, "error saving masto app", http.StatusUnprocessableEntity)
 41		return
 42	}
 43
 44	j := junk.New()
 45	j["id"] = fmt.Sprintf("%d", snowflake())
 46	j["website"] = website
 47	j["name"] = clientName
 48	j["redirect_uri"] = redirectURI
 49	j["client_id"] = clientID
 50	j["client_secret"] = clientSecret
 51	j["vapid_key"] = vapidKey
 52
 53	fmt.Println(j.ToString())
 54	goodjunk(rw, j)
 55}
 56
 57// https://docs.joinmastodon.org/methods/oauth/#authorize
 58func oauthorize(rw http.ResponseWriter, r *http.Request) {
 59	clientID := r.Form.Get("client_id")
 60	redirectURI := r.Form.Get("redirect_uri")
 61
 62	fmt.Println("redirectURI", redirectURI)
 63
 64	_, err := stmtCheckClientId.Exec(clientID)
 65	if err != nil {
 66		elog.Println("oauth: no such client:", clientID)
 67		rw.WriteHeader(http.StatusUnauthorized)
 68		return
 69	}
 70
 71	var nrw NotResponseWriter
 72	login.LoginFunc(&nrw, r)
 73
 74	_, err = stmtSaveMastoAppToken.Exec(nrw.auth)
 75	if err != nil {
 76		elog.Println("oauth: failed to save masto app token", err)
 77		rw.WriteHeader(http.StatusInternalServerError)
 78		return
 79	}
 80
 81	http.Redirect(rw, r, redirectURI+"?code="+nrw.auth, http.StatusFound)
 82}
 83
 84// https://docs.joinmastodon.org/methods/instance/#v2
 85func instance(rw http.ResponseWriter, r *http.Request) {
 86	j := junk.New()
 87
 88	var servername string
 89	if err := getconfig("servername", &servername); err != nil {
 90		http.Error(rw, "getting servername", http.StatusInternalServerError)
 91		return
 92	}
 93
 94	j["uri"] = servername
 95	j["title"] = "honk"
 96	j["description"] = "federated honk conveyance"
 97	j["version"] = "develop"
 98
 99	thumbnail := junk.New()
100	thumbnail["url"] = fmt.Sprintf("https://%s/icon.png", servername)
101	j["thumbnail"] = thumbnail
102	j["languages"] = []string{"en"}
103
104	config := junk.New()
105
106	a := junk.New()
107	a["max_featured_tags"] = 10
108	config["accounts"] = a
109
110	s := junk.New()
111	s["max_characters"] = 5000
112	s["max_media_attachments"] = 1
113	s["characters_reserved_per_url"] = 23
114	config["statuses"] = s
115
116	m := junk.New()
117	m["supported_mime_types"] = []string{
118		"image/jpeg",
119		"image/png",
120		"image/gif",
121		"image/heic",
122		"image/heif",
123		"image/webp",
124		"image/avif",
125		"video/webm",
126		"video/mp4",
127		"video/quicktime",
128		"video/ogg",
129		"audio/wave",
130		"audio/wav",
131		"audio/x-wav",
132		"audio/x-pn-wave",
133		"audio/vnd.wave",
134		"audio/ogg",
135		"audio/vorbis",
136		"audio/mpeg",
137		"audio/mp3",
138		"audio/webm",
139		"audio/flac",
140		"audio/aac",
141		"audio/m4a",
142		"audio/x-m4a",
143		"audio/mp4",
144		"audio/3gpp",
145		"video/x-ms-asf",
146	}
147
148	m["image_size_limit"] = 10485760
149	m["image_matrix_limit"] = 16777216
150	m["video_size_limit"] = 41943040
151	m["video_frame_rate_limit"] = 60
152	m["video_matrix_limit"] = 2304000
153	j["media_attachments"] = m
154
155	rw.WriteHeader(http.StatusOK)
156	rw.Write(j.ToBytes())
157
158	return
159}