forked from microsoft/kiota
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Authentication for Ruby (microsoft#1760)
* Implemented allowedhostsvalidator class and made a couple changes to base bearer file * Small fix to line 23 * Final commit for this branch * auth commit 😝 * update * update * update * update * update * update * update * update * update * update * update to gemfile * update * Gemfile update. * update * update * update * update * Updated Changelog to Reflect Ruby Auth Additions * Commiting some of the changes requested by Philip * Refactoring and Reorganization of Authentication * Changed access token provider constructor * Fixing the require statements in access token p * Deleting outdated tests * Changes/update to Access Token Provider Interface * Updating changelog, gemspec, and readme * Removing include statement * Update abstractions/ruby/microsoft_kiota_abstractions/README.md Co-authored-by: Vincent Biret <vibiret@microsoft.com> * Updating Custom Context * Update abstractions/ruby/microsoft_kiota_abstractions/lib/microsoft_kiota_abstractions/authentication/allowed_hosts_validator.rb Co-authored-by: Philip Gichuhi <pgichuhi@microsoft.com> * Implementing some requested changes * Updates to tests to reflect valid host hash * Changing Package Name * - adds a workflow for ruby authentication package * - fixes link to badge for ruby authentication package * Update authentication-ruby-oauth.yml * Deleting the Azure Folder * Adding Auth folder with the name oauth * Update authentication/ruby/oauth/microsoft_kiota_authentication_oauth/lib/microsoft_kiota_authentication_oauth/oauth_access_token_provider.rb Co-authored-by: Philip Gichuhi <pgichuhi@microsoft.com> * Updating scopes on authcode flow * Updating oauth access token provider * Updating contexts * Update to mimic interfaces in custom oauth flow Co-authored-by: Vincent Biret <vibiret@microsoft.com> Co-authored-by: Philip Gichuhi <pgichuhi@microsoft.com>
- Loading branch information
1 parent
c261e9d
commit f9c5778
Showing
28 changed files
with
829 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
name: Ruby serialization | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: [ main ] | ||
paths: ['authentication/ruby/**', '.github/workflows/**'] | ||
pull_request: | ||
paths: ['authentication/ruby/**', '.github/workflows/**'] | ||
|
||
jobs: | ||
build: | ||
env: | ||
relativePath: ./authentication/ruby/oauth/microsoft_kiota_authentication_oauth | ||
BUNDLE_HTTPS://RUBYGEMS__PKG__GITHUB__COM/MICROSOFT/: "${{ secrets.PUBLISH_GH_USERNAME }}:${{ secrets.PUBLISH_GH_TOKEN }}" | ||
BUNDLE_RUBYGEMS__PKG__GITHUB__COM: "${{ secrets.PUBLISH_GH_USERNAME }}:${{ secrets.PUBLISH_GH_TOKEN }}" #needed for jruby-head | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
os: [ubuntu-latest, macos-latest] | ||
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0' | ||
ruby-version: [2.5, 2.6, 2.7, '3.0', head, jruby, jruby-head, truffleruby, truffleruby-head] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: ${{ matrix.ruby-version }} | ||
bundler-cache: true # runs 'bundle install' and caches installed gems automatically | ||
bundler: 'latest' | ||
cache-version: 1 | ||
working-directory: ${{ env.relativePath }} | ||
- name: Run tests | ||
run: bundle exec rake | ||
working-directory: ${{ env.relativePath }} | ||
- name: Upload artifacts for ruby version 3 and ubuntu | ||
if: ${{ matrix.os == 'ubuntu-latest' && matrix.ruby-version == '3.0'}} | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: drop | ||
path: | | ||
${{ env.relativePath }}/Gemfile.lock | ||
${{ env.relativePath }}/README.md | ||
deploy: | ||
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} | ||
environment: | ||
name: staging_feeds | ||
runs-on: ubuntu-latest | ||
env: | ||
relativePath: ./authentication/ruby/oauth/microsoft_kiota_authentication_oauth | ||
BUNDLE_HTTPS://RUBYGEMS__PKG__GITHUB__COM/MICROSOFT/: "${{ secrets.PUBLISH_GH_USERNAME }}:${{ secrets.PUBLISH_GH_TOKEN }}" | ||
needs: [build] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: ruby/setup-ruby@v1 | ||
with: | ||
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0' | ||
ruby-version: '3.0' | ||
bundler-cache: true # runs 'bundle install' and caches installed gems automatically | ||
bundler: 'latest' | ||
cache-version: 1 | ||
working-directory: ${{ env.relativePath }} | ||
- run: bundle exec rake | ||
working-directory: ${{ env.relativePath }} | ||
- name: Publish to GPR | ||
run: | | ||
mkdir -p $HOME/.gem | ||
touch $HOME/.gem/credentials | ||
chmod 0600 $HOME/.gem/credentials | ||
printf -- "---\n:github: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials | ||
gem build *.gemspec | ||
gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem | ||
working-directory: ${{ env.relativePath }} | ||
env: | ||
GEM_HOST_API_KEY: "Bearer ${{secrets.PUBLISH_GH_TOKEN}}" | ||
OWNER: ${{ github.repository_owner }} | ||
# deploy_prod: | ||
# if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} | ||
# environment: | ||
# name: production_feeds | ||
# runs-on: ubuntu-latest | ||
# env: | ||
# relativePath: ./authentication/ruby/oauth/microsoft_kiota_authentication_oauth | ||
# BUNDLE_HTTPS://RUBYGEMS__PKG__GITHUB__COM/MICROSOFT/: "${{ secrets.PUBLISH_GH_USERNAME }}:${{ secrets.PUBLISH_GH_TOKEN }}" | ||
# needs: [build] | ||
# steps: | ||
# - uses: actions/checkout@v3 | ||
# - uses: ruby/setup-ruby@v1 | ||
# with: | ||
# # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0' | ||
# ruby-version: '3.0' | ||
# bundler-cache: true # runs 'bundle install' and caches installed gems automatically | ||
# - run: bundle exec rake | ||
# - name: Publish to RubyGems | ||
# run: | | ||
# mkdir -p $HOME/.gem | ||
# touch $HOME/.gem/credentials | ||
# chmod 0600 $HOME/.gem/credentials | ||
# printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials | ||
# gem build *.gemspec | ||
# gem push *.gem | ||
# env: | ||
# GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}" TODO: Token for rubyGems.org | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...ota_abstractions/lib/microsoft_kiota_abstractions/authentication/access_token_provider.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'concurrent' | ||
require_relative 'allowed_hosts_validator' | ||
|
||
module MicrosoftKiotaAbstractions | ||
# Access Token Provider Module implementation | ||
module AccessTokenProvider | ||
# This function obtains the authorization token. | ||
# :params | ||
# uri: a string containing the uri | ||
# additional_params: hash of symbols to string values, ie { response_mode: 'fragment', prompt: 'login' } | ||
# default is empty hash | ||
def get_authorization_token(uri, additional_properties = {}) | ||
raise NotImplementedError.new | ||
end | ||
|
||
attr_accessor :scopes, :host_validator | ||
|
||
end | ||
end |
37 changes: 37 additions & 0 deletions
37
...a_abstractions/lib/microsoft_kiota_abstractions/authentication/allowed_hosts_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'uri' | ||
|
||
module MicrosoftKiotaAbstractions | ||
# Maintains a list of valid hosts and allows authentication providers to check whether | ||
# a host is valid before authenticating a request | ||
class AllowedHostsValidator | ||
# creates a new AllocatedHostsValidator with provided values | ||
def initialize(allowed_hosts) | ||
@allowed_hosts = {} | ||
allowed_hosts.each { |host| @allowed_hosts[host.downcase] = true } | ||
end | ||
|
||
# sets the list of valid hosts with provided value (val) | ||
def allowed_hosts=(val) | ||
@allowed_hosts = {} | ||
val.each { |host| @allowed_hosts[host.downcase] = true } | ||
end | ||
|
||
# checks whether the provided host is valid | ||
def url_host_valid?(url) | ||
return false unless url =~ URI::DEFAULT_PARSER.regexp[:ABS_URI] | ||
|
||
return true if @allowed_hosts.empty? | ||
|
||
parsed_url = URI(url) | ||
|
||
return false if parsed_url.host.nil? | ||
|
||
@allowed_hosts.key? parsed_url.host.downcase | ||
end | ||
|
||
# gets the list of valid hosts | ||
attr_reader :allowed_hosts | ||
end | ||
end |
6 changes: 2 additions & 4 deletions
6
...a_abstractions/lib/microsoft_kiota_abstractions/authentication/authentication_provider.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
module MicrosoftKiotaAbstractions | ||
module AuthenticationProvider | ||
|
||
def authenticate_request(request) | ||
def authenticate_request(request, additional_properties = {}) | ||
raise NotImplementedError.new | ||
end | ||
|
||
end | ||
end | ||
end |
34 changes: 18 additions & 16 deletions
34
.../microsoft_kiota_abstractions/authentication/base_bearer_token_authentication_provider.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'concurrent' | ||
require_relative './authentication_provider' | ||
require_relative './access_token_provider' | ||
|
||
module MicrosoftKiotaAbstractions | ||
class BaseBearerTokenAuthenticationProvider | ||
# Provides a base class for implementing AuthenticationProvider for Bearer token scheme | ||
class BaseBearerTokenAuthenticationProvider | ||
include MicrosoftKiotaAbstractions::AccessTokenProvider | ||
include MicrosoftKiotaAbstractions::AuthenticationProvider | ||
include Concurrent::Async | ||
def initialize(access_token_provider) | ||
raise StandardError, 'access_token_provider parameter cannot be nil' if access_token_provider.nil? | ||
|
||
@access_token_provider = access_token_provider | ||
end | ||
|
||
AUTHORIZATION_HEADER_KEY = 'Authorization' | ||
def authenticate_request(request) | ||
if !request | ||
raise StandardError, 'request cannot be null' | ||
end | ||
if !request.headers.has_key?(AUTHORIZATION_HEADER_KEY) | ||
token = self.get_authorization_token(request) | ||
if !token | ||
raise StandardError, 'Could not get an authorization token' | ||
end | ||
request.headers[AUTHORIZATION_HEADER_KEY] = 'Bearer ' + token | ||
end | ||
end | ||
def authenticate_request(request, additional_properties) | ||
raise StandardError, 'Request cannot be null' if request.nil? | ||
return if request.headers.key?(AUTHORIZATION_HEADER_KEY) | ||
|
||
token = @access_token_provider.get_authorization_token(request, additional_properties) | ||
|
||
def get_authorization_token(request) | ||
raise NotImplementedError, 'get_authorization_token must be implemented' | ||
request.headers[AUTHORIZATION_HEADER_KEY] = "Bearer #{token}" unless token.nil? | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
authentication/ruby/oauth/microsoft_kiota_authentication_oauth/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/.bundle/ | ||
/.yardoc | ||
/_yardoc/ | ||
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
|
||
# rspec failure tracking | ||
.rspec_status |
24 changes: 24 additions & 0 deletions
24
authentication/ruby/oauth/microsoft_kiota_authentication_oauth/Gemfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in microsoft_kiota_authentication.gemspec | ||
gemspec | ||
|
||
# git_source(:github) { |repo_name| "https://rubygems.pkg.github.com/microsoft" } | ||
|
||
source "https://rubygems.pkg.github.com/microsoft" do | ||
gem "microsoft_kiota_abstractions", "0.2.0" | ||
end | ||
|
||
gem 'rake', '~> 13.0' | ||
|
||
gem 'rspec', '~> 3.0' | ||
|
||
gem 'rubocop', require: false | ||
|
||
gem 'concurrent-ruby', '~> 1.1', '>= 1.1.9' | ||
|
||
gem 'addressable', '~> 2.7', '>= 2.7.0' | ||
|
||
gem "oauth2", "~> 2.0" |
Oops, something went wrong.