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