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 {...@@ -130,20 +130,20 @@ pub const providers = struct {
130130
131pub const dynamic_providers = struct {131pub const dynamic_providers = struct {
132 pub const _gitea = Provider{132 pub const _gitea = Provider{
133 .id = "_gitea",133 .id = "gitea",
134 .authorize_url = "https://{domain}/login/oauth/authorize",134 .authorize_url = "https://{[domain]s}/login/oauth/authorize",
135 .token_url = "https://{domain}/login/oauth/access_token",135 .token_url = "https://{[domain]s}/login/oauth/access_token",
136 .me_url = "https://{domain}/api/v1/user",136 .me_url = "https://{[domain]s}/api/v1/user",
137 .name_prop = "username",137 .name_prop = "username",
138 .name_prefix = "@",138 .name_prefix = "@",
139 .logo = icon_url("gitea"),139 .logo = icon_url("gitea"),
140 .color = "#609926",140 .color = "#609926",
141 };141 };
142 pub const _gitlab = Provider{142 pub const _gitlab = Provider{
143 .id = "_gitlab",143 .id = "gitlab",
144 .authorize_url = "https://{domain}/oauth/authorize",144 .authorize_url = "https://{[domain]s}/oauth/authorize",
145 .token_url = "https://{domain}/oauth/token",145 .token_url = "https://{[domain]s}/oauth/token",
146 .me_url = "https://{domain}/api/v4/user",146 .me_url = "https://{[domain]s}/api/v4/user",
147 .scope = "read_user",147 .scope = "read_user",
148 .name_prop = "username",148 .name_prop = "username",
149 .name_prefix = "@",149 .name_prefix = "@",
...@@ -151,10 +151,10 @@ pub const dynamic_providers = struct {...@@ -151,10 +151,10 @@ pub const dynamic_providers = struct {
151 .color = "#FCA121",151 .color = "#FCA121",
152 };152 };
153 pub const _mastodon = Provider{153 pub const _mastodon = Provider{
154 .id = "_mastodon",154 .id = "mastodon",
155 .authorize_url = "https://{domain}/oauth/authorize",155 .authorize_url = "https://{[domain]s}/oauth/authorize",
156 .token_url = "https://{domain}/oauth/token",156 .token_url = "https://{[domain]s}/oauth/token",
157 .me_url = "https://{domain}/api/v1/accounts/verify_credentials",157 .me_url = "https://{[domain]s}/api/v1/accounts/verify_credentials",
158 .scope = "read:accounts",158 .scope = "read:accounts",
159 .name_prop = "username",159 .name_prop = "username",
160 .name_prefix = "@",160 .name_prefix = "@",
...@@ -162,10 +162,10 @@ pub const dynamic_providers = struct {...@@ -162,10 +162,10 @@ pub const dynamic_providers = struct {
162 .color = "#3088D4",162 .color = "#3088D4",
163 };163 };
164 pub const _pleroma = Provider{164 pub const _pleroma = Provider{
165 .id = "_pleroma",165 .id = "pleroma",
166 .authorize_url = "https://{domain}/oauth/authorize",166 .authorize_url = "https://{[domain]s}/oauth/authorize",
167 .token_url = "https://{domain}/oauth/token",167 .token_url = "https://{[domain]s}/oauth/token",
168 .me_url = "https://{domain}/api/v1/accounts/verify_credentials",168 .me_url = "https://{[domain]s}/api/v1/accounts/verify_credentials",
169 .scope = "read:accounts",169 .scope = "read:accounts",
170 .name_prop = "username",170 .name_prop = "username",
171 .name_prefix = "@",171 .name_prefix = "@",
...@@ -174,13 +174,34 @@ pub const dynamic_providers = struct {...@@ -174,13 +174,34 @@ pub const dynamic_providers = struct {
174 };174 };
175};175};
176176
177pub fn providerById(name: string) ?Provider {177pub fn providerById(alloc: std.mem.Allocator, name: string) !?Provider {
178 inline for (std.meta.declarations(providers)) |item| {178 inline for (std.meta.declarations(providers)) |item| {
179 const p = @field(providers, item.name);179 const p = @field(providers, item.name);
180 if (std.mem.eql(u8, p.id, name)) {180 if (std.mem.eql(u8, p.id, name)) {
181 return p;181 return p;
182 }182 }
183 }183 }
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 }
184 return null;205 return null;
185}206}
186207