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