diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..71969716e3e9ca87fdafa1839c25c6daf7da7560 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +zig-* +.zigmod +deps.zig diff --git a/build.zig b/build.zig new file mode 100644 index 0000000000000000000000000000000000000000..35e1d846362f5419e20c5e33e066067d996e5fb0 --- /dev/null +++ b/build.zig @@ -0,0 +1,29 @@ +const std = @import("std"); +const deps = @import("./deps.zig"); + +pub fn build(b: *std.build.Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("zig-oauth2", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + deps.addAllTo(exe); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/src/lib.zig b/src/lib.zig new file mode 100644 index 0000000000000000000000000000000000000000..8cd33724cdb41fde19c2d5f4478ab20445f4330a --- /dev/null +++ b/src/lib.zig @@ -0,0 +1,157 @@ +// https://oauth.net/2/ +// +const std = @import("std"); +const string = []const u8; + +pub const Provider = struct { + id: string, + authorize_url: string, + token_url: string, + me_url: string, + scope: string = "", + name_prop: string, + name_prefix: string = "", + id_prop: string = "id", + logo: string, + color: string, +}; + +pub const Client = struct { + provider: Provider, + id: string, + secret: string, +}; + +pub const providers = struct { + fn icon_url(comptime name: string) string { + return "https://unpkg.com/simple-icons@" ++ "5.13.0" ++ "/icons/" ++ name ++ ".svg"; + } + + pub var amazon = Provider{ + .id = "amazon", + .authorize_url = "https://www.amazon.com/ap/oa", + .token_url = "https://api.amazon.com/auth/o2/token", + .me_url = "https://api.amazon.com/user/profile", + .scope = "profile", + .name_prop = "name", + .id_prop = "user_id", + .logo = icon_url("amazon"), + .color = "#FF9900", + }; + pub var battle_net = Provider{ + .id = "battle.net", + .authorize_url = "https://us.battle.net/oauth/authorize", + .token_url = "https://us.battle.net/oauth/token", + .me_url = "https://us.battle.net/oauth/userinfo", + .scope = "openid", + .name_prop = "battletag", + .logo = icon_url("battle-dot-net"), + .color = "#00AEFF", + }; + pub var discord = Provider{ + .id = "discord", + .authorize_url = "https://discordapp.com/api/oauth2/authorize", + .token_url = "https://discordapp.com/api/oauth2/token", + .me_url = "https://discordapp.com/api/users/@me", + .scope = "identify", + .name_prop = "username", + .name_prefix = "@", + .logo = icon_url("discord"), + .color = "#7289DA", + }; + pub var facebook = Provider{ + .id = "facebook", + .authorize_url = "https://graph.facebook.com/oauth/authorize", + .token_url = "https://graph.facebook.com/oauth/access_token", + .me_url = "https://graph.facebook.com/me", + .name_prop = "name", + .logo = icon_url("facebook"), + .color = "#1877F2", + }; + pub var github = Provider{ + .id = "github", + .authorize_url = "https://github.com/login/oauth/authorize", + .token_url = "https://github.com/login/oauth/access_token", + .me_url = "https://api.github.com/user", + .scope = "read:user", + .name_prop = "login", + .name_prefix = "@", + .logo = icon_url("github"), + .color = "#181717", + }; + pub var google = Provider{ + .id = "google", + .authorize_url = "https://accounts.google.com/o/oauth2/v2/auth", + .token_url = "https://www.googleapis.com/oauth2/v4/token", + .me_url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json", + .scope = "profile", + .name_prop = "name", + .logo = icon_url("google"), + .color = "#4285F4", + }; + pub var microsoft = Provider{ + .id = "microsoft", + .authorize_url = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize", + .token_url = "https://login.microsoftonline.com/common/oauth2/v2.0/token", + .me_url = "https://graph.microsoft.com/v1.0/me/", + .scope = "https://graph.microsoft.com/user.read", + .name_prop = "displayName", + .logo = icon_url("microsoft"), + .color = "#666666", + }; + pub var reddit = Provider{ + .id = "reddit", + .authorize_url = "https://old.reddit.com/api/v1/authorize", + .token_url = "https://old.reddit.com/api/v1/access_token", + .me_url = "https://oauth.reddit.com/api/v1/me", + .scope = "identity", + .name_prop = "name", + .name_prefix = "u/", + .logo = icon_url("reddit"), + .color = "#FF4500", + }; + + pub var _gitea = Provider{ + .id = "_gitea", + .authorize_url = "https://{domain}/login/oauth/authorize", + .token_url = "https://{domain}/login/oauth/access_token", + .me_url = "https://{domain}/api/v1/user", + .name_prop = "username", + .name_prefix = "@", + .logo = icon_url("gitea"), + .color = "#609926", + }; + pub var _gitlab = Provider{ + .id = "_gitlab", + .authorize_url = "https://{domain}/oauth/authorize", + .token_url = "https://{domain}/oauth/token", + .me_url = "https://{domain}/api/v4/user", + .scope = "read_user", + .name_prop = "username", + .name_prefix = "@", + .logo = icon_url("gitlab"), + .color = "#FCA121", + }; + pub var _mastodon = Provider{ + .id = "_mastodon", + .authorize_url = "https://{domain}/oauth/authorize", + .token_url = "https://{domain}/oauth/token", + .me_url = "https://{domain}/api/v1/accounts/verify_credentials", + .scope = "read:accounts", + .name_prop = "username", + .name_prefix = "@", + .logo = icon_url("mastodon"), + .color = "#3088D4", + }; + pub var _pleroma = Provider{ + .id = "_pleroma", + .authorize_url = "https://{domain}/oauth/authorize", + .token_url = "https://{domain}/oauth/token", + .me_url = "https://{domain}/api/v1/accounts/verify_credentials", + .scope = "read:accounts", + .name_prop = "username", + .name_prefix = "@", + .logo = icon_url("pleroma"), + .color = "#FBA457", + }; +}; diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000000000000000000000000000000000000..d29869ff88feb94cd5a5fd28d760824920f269ac --- /dev/null +++ b/src/main.zig @@ -0,0 +1,5 @@ +const std = @import("std"); + +pub fn main() anyerror!void { + std.log.info("All your codebase are belong to us.", .{}); +} diff --git a/zig.mod b/zig.mod new file mode 100644 index 0000000000000000000000000000000000000000..cb8d9a91e22bd893317987946c11226e9f7a8834 --- /dev/null +++ b/zig.mod @@ -0,0 +1,6 @@ +id: raz0lqnollhuhz5bq5n57pbo8cwm9l55z475i4ufcpzl57ez +name: oauth2 +main: src/lib.zig +license: MIT +description: HTTP handler functions to allow you to easily add OAuth2 login support to your Zig application +dependencies: diff --git a/zigmod.lock b/zigmod.lock new file mode 100644 index 0000000000000000000000000000000000000000..0cfbf08886fca9a91cb753ec8734c84fcbe52c9f --- /dev/null +++ b/zigmod.lock @@ -0,0 +1 @@ +2