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