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