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 _, err := stmtCheckClientId.Exec(clientID)
63 if err != nil {
64 elog.Println("oauth: no such client:", clientID)
65 rw.WriteHeader(http.StatusUnauthorized)
66 return
67 }
68
69 var nrw NotResponseWriter
70 login.LoginFunc(&nrw, r)
71
72 _, err = stmtSaveMastoAppToken.Exec(nrw.auth)
73 if err != nil {
74 elog.Println("oauth: failed to save masto app token", err)
75 rw.WriteHeader(http.StatusInternalServerError)
76 return
77 }
78
79 http.Redirect(rw, r, redirectURI+"?code="+nrw.auth, http.StatusFound)
80}
81
82// https://docs.joinmastodon.org/methods/instance/#v2
83func instance(rw http.ResponseWriter, r *http.Request) {
84 j := junk.New()
85
86 var servername string
87 if err := getconfig("servername", &servername); err != nil {
88 http.Error(rw, "getting servername", http.StatusInternalServerError)
89 return
90 }
91
92 j["uri"] = servername
93 j["title"] = "honk"
94 j["description"] = "federated honk conveyance"
95 j["version"] = "develop"
96
97 thumbnail := junk.New()
98 thumbnail["url"] = fmt.Sprintf("https://%s/icon.png", servername)
99 j["thumbnail"] = thumbnail
100 j["languages"] = []string{"en"}
101
102 config := junk.New()
103
104 a := junk.New()
105 a["max_featured_tags"] = 10
106 config["accounts"] = a
107
108 s := junk.New()
109 s["max_characters"] = 5000
110 s["max_media_attachments"] = 1
111 s["characters_reserved_per_url"] = 23
112 config["statuses"] = s
113
114 m := junk.New()
115 m["supported_mime_types"] = []string{
116 "image/jpeg",
117 "image/png",
118 "image/gif",
119 "image/heic",
120 "image/heif",
121 "image/webp",
122 "image/avif",
123 "video/webm",
124 "video/mp4",
125 "video/quicktime",
126 "video/ogg",
127 "audio/wave",
128 "audio/wav",
129 "audio/x-wav",
130 "audio/x-pn-wave",
131 "audio/vnd.wave",
132 "audio/ogg",
133 "audio/vorbis",
134 "audio/mpeg",
135 "audio/mp3",
136 "audio/webm",
137 "audio/flac",
138 "audio/aac",
139 "audio/m4a",
140 "audio/x-m4a",
141 "audio/mp4",
142 "audio/3gpp",
143 "video/x-ms-asf",
144 }
145
146 m["image_size_limit"] = 10485760
147 m["image_matrix_limit"] = 16777216
148 m["video_size_limit"] = 41943040
149 m["video_frame_rate_limit"] = 60
150 m["video_matrix_limit"] = 2304000
151 j["media_attachments"] = m
152
153 rw.WriteHeader(http.StatusOK)
154 rw.Write(j.ToBytes())
155
156 return
157}