authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-01-24 20:48:00 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-01-24 20:48:00 -08:00
log3aca350ab15651d3b0085ec0657e069f45dc29fc
tree9803c6087789737445610ba56fca3c475f4f12ee
parentd4adef77a2f4388ffed1bf1ed5b2813f5608ee9e

fix providerById to support dynamic providers


1 files changed, 38 insertions(+), 17 deletions(-)

src/lib.zig+38-17
......@@ -130,20 +130,20 @@ pub const providers = struct {
130130
131131pub const dynamic_providers = struct {
132132 pub const _gitea = Provider{
133 .id = "_gitea",
134 .authorize_url = "https://{domain}/login/oauth/authorize",
135 .token_url = "https://{domain}/login/oauth/access_token",
136 .me_url = "https://{domain}/api/v1/user",
133 .id = "gitea",
134 .authorize_url = "https://{[domain]s}/login/oauth/authorize",
135 .token_url = "https://{[domain]s}/login/oauth/access_token",
136 .me_url = "https://{[domain]s}/api/v1/user",
137137 .name_prop = "username",
138138 .name_prefix = "@",
139139 .logo = icon_url("gitea"),
140140 .color = "#609926",
141141 };
142142 pub const _gitlab = Provider{
143 .id = "_gitlab",
144 .authorize_url = "https://{domain}/oauth/authorize",
145 .token_url = "https://{domain}/oauth/token",
146 .me_url = "https://{domain}/api/v4/user",
143 .id = "gitlab",
144 .authorize_url = "https://{[domain]s}/oauth/authorize",
145 .token_url = "https://{[domain]s}/oauth/token",
146 .me_url = "https://{[domain]s}/api/v4/user",
147147 .scope = "read_user",
148148 .name_prop = "username",
149149 .name_prefix = "@",
......@@ -151,10 +151,10 @@ pub const dynamic_providers = struct {
151151 .color = "#FCA121",
152152 };
153153 pub const _mastodon = Provider{
154 .id = "_mastodon",
155 .authorize_url = "https://{domain}/oauth/authorize",
156 .token_url = "https://{domain}/oauth/token",
157 .me_url = "https://{domain}/api/v1/accounts/verify_credentials",
154 .id = "mastodon",
155 .authorize_url = "https://{[domain]s}/oauth/authorize",
156 .token_url = "https://{[domain]s}/oauth/token",
157 .me_url = "https://{[domain]s}/api/v1/accounts/verify_credentials",
158158 .scope = "read:accounts",
159159 .name_prop = "username",
160160 .name_prefix = "@",
......@@ -162,10 +162,10 @@ pub const dynamic_providers = struct {
162162 .color = "#3088D4",
163163 };
164164 pub const _pleroma = Provider{
165 .id = "_pleroma",
166 .authorize_url = "https://{domain}/oauth/authorize",
167 .token_url = "https://{domain}/oauth/token",
168 .me_url = "https://{domain}/api/v1/accounts/verify_credentials",
165 .id = "pleroma",
166 .authorize_url = "https://{[domain]s}/oauth/authorize",
167 .token_url = "https://{[domain]s}/oauth/token",
168 .me_url = "https://{[domain]s}/api/v1/accounts/verify_credentials",
169169 .scope = "read:accounts",
170170 .name_prop = "username",
171171 .name_prefix = "@",
......@@ -174,13 +174,34 @@ pub const dynamic_providers = struct {
174174 };
175175};
176176
177pub fn providerById(name: string) ?Provider {
177pub fn providerById(alloc: std.mem.Allocator, name: string) !?Provider {
178178 inline for (std.meta.declarations(providers)) |item| {
179179 const p = @field(providers, item.name);
180180 if (std.mem.eql(u8, p.id, name)) {
181181 return p;
182182 }
183183 }
184 const c_ind = std.mem.indexOfScalar(u8, name, ',') orelse return null;
185 const p_id = name[0..c_ind];
186 const domain = name[c_ind + 1 ..];
187 const args = .{ .domain = domain };
188 inline for (std.meta.declarations(dynamic_providers)) |item| {
189 const didp = @field(dynamic_providers, item.name);
190 if (std.mem.eql(u8, didp.id, p_id)) {
191 return Provider{
192 .id = name,
193 .authorize_url = try std.fmt.allocPrint(alloc, didp.authorize_url, args),
194 .token_url = try std.fmt.allocPrint(alloc, didp.token_url, args),
195 .me_url = try std.fmt.allocPrint(alloc, didp.me_url, args),
196 .scope = didp.scope,
197 .name_prop = didp.name_prop,
198 .name_prefix = didp.name_prefix,
199 .id_prop = didp.id_prop,
200 .logo = didp.logo,
201 .color = didp.color,
202 };
203 }
204 }
184205 return null;
185206}
186207