From 559f17774e5d731847ba1f2461c6fcebdca6d8e3 Mon Sep 17 00:00:00 2001 From: Dario P Date: Sun, 25 Aug 2024 18:12:18 +0200 Subject: [PATCH 1/4] implement code signatures --- lib/code0/identities/provider/base_oauth.rb | 2 +- sig/code0/identities.rbs | 2 +- sig/code0/identities/identity.rbs | 14 +++++++++++++ sig/code0/identities/identity_provider.rbs | 18 +++++++++++++++++ sig/code0/identities/provider/base_oauth.rbs | 21 ++++++++++++++++++++ sig/code0/identities/provider/discord.rbs | 17 ++++++++++++++++ sig/code0/identities/provider/github.rbs | 19 ++++++++++++++++++ sig/code0/identities/provider/gitlab.rbs | 19 ++++++++++++++++++ sig/code0/identities/provider/google.rbs | 19 ++++++++++++++++++ sig/code0/identities/provider/microsoft.rbs | 17 ++++++++++++++++ sig/code0/identities/version.rbs | 5 +++++ 11 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 sig/code0/identities/identity.rbs create mode 100644 sig/code0/identities/identity_provider.rbs create mode 100644 sig/code0/identities/provider/base_oauth.rbs create mode 100644 sig/code0/identities/provider/discord.rbs create mode 100644 sig/code0/identities/provider/github.rbs create mode 100644 sig/code0/identities/provider/gitlab.rbs create mode 100644 sig/code0/identities/provider/google.rbs create mode 100644 sig/code0/identities/provider/microsoft.rbs create mode 100644 sig/code0/identities/version.rbs diff --git a/lib/code0/identities/provider/base_oauth.rb b/lib/code0/identities/provider/base_oauth.rb index b02eb16..995e132 100644 --- a/lib/code0/identities/provider/base_oauth.rb +++ b/lib/code0/identities/provider/base_oauth.rb @@ -63,7 +63,7 @@ def check_response(response) raise Error, response.body end - def create_identity(*) + def create_identity(response, token, token_type) raise NotImplementedError end diff --git a/sig/code0/identities.rbs b/sig/code0/identities.rbs index f195ac5..5f88fd5 100644 --- a/sig/code0/identities.rbs +++ b/sig/code0/identities.rbs @@ -1,5 +1,5 @@ module Code0 module Identities - VERSION: String + class Error < StandardError end end end diff --git a/sig/code0/identities/identity.rbs b/sig/code0/identities/identity.rbs new file mode 100644 index 0000000..9557429 --- /dev/null +++ b/sig/code0/identities/identity.rbs @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Code0 + module Identities + class Identity < Struct[String] + attr_reader provider: Symbol + attr_reader identitfier: String + attr_reader username: String? + attr_reader email: String? + attr_reader firstname: String? + attr_reader lastname: String? + end + end +end diff --git a/sig/code0/identities/identity_provider.rbs b/sig/code0/identities/identity_provider.rbs new file mode 100644 index 0000000..68c725a --- /dev/null +++ b/sig/code0/identities/identity_provider.rbs @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Code0 + module Identities + class IdentityProvider + attr_reader providers: Array[Provider::BaseOauth] + + def initialize: () -> void + + def add_provider: (provider_type: Symbol, config: Proc | Hash[Symbol, any]) -> void + + def add_named_provider: (provider_id: Symbol, provider_type: Symbol, config: Proc | Hash[Symbol, any]) -> void + + def load_identity: (provider_id: Symbol, params: Hash[Symbol, any]) -> Identity + + end + end +end diff --git a/sig/code0/identities/provider/base_oauth.rbs b/sig/code0/identities/provider/base_oauth.rbs new file mode 100644 index 0000000..552eed9 --- /dev/null +++ b/sig/code0/identities/provider/base_oauth.rbs @@ -0,0 +1,21 @@ +module Code0 + module Identities + module Provider + class BaseOauth + attr_reader config_loader: Proc | Hash[Symbol, any] + + def initialize: (config_loader: Proc | Hash[Symbol, any]) -> void + + def authorization_url: () -> String + + def token_url: () -> String + + def user_details_url: () -> String + + def config: () -> Hash[Symbol, any] + + def create_identity: (response: Net::HTTPResponse, token: String, token_type: String) -> Identity + end + end + end +end diff --git a/sig/code0/identities/provider/discord.rbs b/sig/code0/identities/provider/discord.rbs new file mode 100644 index 0000000..08a0218 --- /dev/null +++ b/sig/code0/identities/provider/discord.rbs @@ -0,0 +1,17 @@ +module Code0 + module Identities + module Provider + class Discord < BaseOauth + def token_url: () -> "https://discord.com/api/oauth2/token" + + def token_payload: (untyped code) -> { code: String, grant_type: "authorization_code", redirect_uri: String, client_id: String, client_secret: String } + + def user_details_url: () -> "https://discord.com/api/users/@me" + + def authorization_url: () -> String + + def create_identity: (response: Net::HTTPResponse) -> Identity + end + end + end +end diff --git a/sig/code0/identities/provider/github.rbs b/sig/code0/identities/provider/github.rbs new file mode 100644 index 0000000..d4ef086 --- /dev/null +++ b/sig/code0/identities/provider/github.rbs @@ -0,0 +1,19 @@ +module Code0 + module Identities + module Provider + class Github < BaseOauth + def token_url: () -> "https://github.com/login/oauth/access_token" + + def token_payload: (code: String) -> { code: String, redirect_uri: String, client_id: String, client_secret: String } + + def user_details_url: () -> "https://api.github.com/user" + + def authorization_url: () -> ::String + + def private_email: (access_token: String, token_type: String) -> String + + def create_identity: (response: Net::HTTPResponse, access_token: String, token_type: String) -> Identity + end + end + end +end diff --git a/sig/code0/identities/provider/gitlab.rbs b/sig/code0/identities/provider/gitlab.rbs new file mode 100644 index 0000000..f8ad700 --- /dev/null +++ b/sig/code0/identities/provider/gitlab.rbs @@ -0,0 +1,19 @@ +module Code0 + module Identities + module Provider + class Gitlab < BaseOauth + def base_url: () -> String + + def token_url: () -> ::String + + def token_payload: (code: String) -> { code: String, grant_type: "authorization_code", redirect_uri: String, client_id: String, client_secret: String } + + def user_details_url: () -> ::String + + def authorization_url: () -> String + + def create_identity: (response: Net::HTTPResponse) -> Identity + end + end + end +end diff --git a/sig/code0/identities/provider/google.rbs b/sig/code0/identities/provider/google.rbs new file mode 100644 index 0000000..4d97288 --- /dev/null +++ b/sig/code0/identities/provider/google.rbs @@ -0,0 +1,19 @@ +module Code0 + module Identities + module Provider + class Google < BaseOauth + def base_url: () -> "https://accounts.google.com" + + def token_url: () -> "https://oauth2.googleapis.com/token" + + def token_payload: (code: String) -> { code: String, grant_type: "authorization_code", redirect_uri: String, client_id: String, client_secret: String } + + def user_details_url: () -> "https://www.googleapis.com/oauth2/v3/userinfo" + + def authorization_url: () -> String + + def create_identity: (response: Net::HTTPResponse) -> Identity + end + end + end +end diff --git a/sig/code0/identities/provider/microsoft.rbs b/sig/code0/identities/provider/microsoft.rbs new file mode 100644 index 0000000..39786d0 --- /dev/null +++ b/sig/code0/identities/provider/microsoft.rbs @@ -0,0 +1,17 @@ +module Code0 + module Identities + module Provider + class Microsoft < BaseOauth + def base_url: () -> "https://graph.microsoft.com/" + + def token_url: () -> "https://login.microsoftonline.com/consumers/oauth2/v2.0/token" + + def token_payload: (code: String) -> { code: String, grant_type: "authorization_code", redirect_uri: String, client_id: String, client_secret: String } + + def user_details_url: () -> "https://graph.microsoft.com/oidc/userinfo" + + def create_identity: (response: Net::HTTPResponse) -> Identity + end + end + end +end diff --git a/sig/code0/identities/version.rbs b/sig/code0/identities/version.rbs new file mode 100644 index 0000000..76b9b93 --- /dev/null +++ b/sig/code0/identities/version.rbs @@ -0,0 +1,5 @@ +module Code0 + module Identities + VERSION: String + end +end \ No newline at end of file From a1111256713facd2142910a41275b061e6222462 Mon Sep 17 00:00:00 2001 From: Dario P Date: Sun, 25 Aug 2024 18:25:11 +0200 Subject: [PATCH 2/4] fix wrong typed attr reader --- sig/code0/identities/identity_provider.rbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sig/code0/identities/identity_provider.rbs b/sig/code0/identities/identity_provider.rbs index 68c725a..6339dcb 100644 --- a/sig/code0/identities/identity_provider.rbs +++ b/sig/code0/identities/identity_provider.rbs @@ -3,7 +3,7 @@ module Code0 module Identities class IdentityProvider - attr_reader providers: Array[Provider::BaseOauth] + attr_reader providers: Hash[Symbol, Provider::BaseOauth] def initialize: () -> void From d15630edf6af5b75358201f5f44760c4731ecabe Mon Sep 17 00:00:00 2001 From: Dario P Date: Thu, 29 Aug 2024 20:57:33 +0200 Subject: [PATCH 3/4] add missing type signatures for base oauth class --- sig/code0/identities/provider/base_oauth.rbs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sig/code0/identities/provider/base_oauth.rbs b/sig/code0/identities/provider/base_oauth.rbs index 552eed9..1a27976 100644 --- a/sig/code0/identities/provider/base_oauth.rbs +++ b/sig/code0/identities/provider/base_oauth.rbs @@ -14,7 +14,18 @@ module Code0 def config: () -> Hash[Symbol, any] + def load_identity: (params: Hash[Symbol, any]) -> Identity + + def token_payload: (code: String) -> Hash[Symbol, any] + def create_identity: (response: Net::HTTPResponse, token: String, token_type: String) -> Identity + + private + + def access_token: (code: String) -> Array[String] + + def check_response: (response: Net::HTTPResponse) -> void + end end end From 629757f082f7c305cb93e67ef1e964a79969a770 Mon Sep 17 00:00:00 2001 From: Dario Pranjic <96529060+Knerio@users.noreply.github.com> Date: Thu, 29 Aug 2024 21:00:11 +0200 Subject: [PATCH 4/4] Fix type sig Co-authored-by: Niklas van Schrick --- sig/code0/identities/provider/discord.rbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sig/code0/identities/provider/discord.rbs b/sig/code0/identities/provider/discord.rbs index 08a0218..b16d747 100644 --- a/sig/code0/identities/provider/discord.rbs +++ b/sig/code0/identities/provider/discord.rbs @@ -4,7 +4,7 @@ module Code0 class Discord < BaseOauth def token_url: () -> "https://discord.com/api/oauth2/token" - def token_payload: (untyped code) -> { code: String, grant_type: "authorization_code", redirect_uri: String, client_id: String, client_secret: String } + def token_payload: (code: String) -> { code: String, grant_type: "authorization_code", redirect_uri: String, client_id: String, client_secret: String } def user_details_url: () -> "https://discord.com/api/users/@me"