usos_auth_lib 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 135ee424533e6fee6163985ab9116940d3809e107ede2ebde5deb42afcf40489
4
+ data.tar.gz: c1a3fc3af4ac488228bb79b8f48919cbd79eccb8ba8a81ab8773285b458134c5
5
+ SHA512:
6
+ metadata.gz: a1d8a55ce5caa3b6a1482d3df93bb9fc03e75289b5b89dd2a51ee83c05a87075fa30c9b0703af57dcd126a198db79ebac69511d7ce90441b2fcf50fde52066d3
7
+ data.tar.gz: 193c8ea2b7907f5e361ce266c50acd78e88d27270ea345313dd247f86af95defd11ad17f62537198c96040e1181df3add71698d1bf15ffd64b128b90871ecb5e
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright mikolajczu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,145 @@
1
+ # UsosAuthLib
2
+ Introducing "UsosAuthLib" a powerful Ruby on Rails library designed to streamline user authentication and seamlessly handle requests through the USOS API. This library simplifies the integration process, allowing developers to effortlessly authenticate users using USOS credentials within their Rails applications. With a focus on security and efficiency, Rails USOS Auth ensures a smooth user experience while providing robust support for USOS API interactions. Elevate your application's functionality by effortlessly incorporating user authentication and USOS API communication with the convenience of Rails USOS Auth.
3
+
4
+ ## USOS API
5
+ The USOS API, a cornerstone of academic data access, opens up a world of possibilities for developers seeking to integrate educational information into their applications. Following the OAuth 1.0a workflow as outlined in the official documentation from 'https://apps.usos.edu.pl/developers/api/authorization/', developers can securely implement user authentication and gain authorized access to the wealth of data stored in the USOS system.
6
+
7
+ The OAuth 1.0a workflow ensures a robust and secure authentication process, safeguarding user credentials while granting seamless access to the USOS API. By adhering to the guidelines provided in the official source, developers can confidently build applications that tap into the extensive educational resources offered by USOS, enriching their projects with academic data in a reliable and user-friendly manner. Explore the possibilities of educational integration through the USOS API and OAuth 1.0a, empowering your applications with a wealth of valuable information.
8
+
9
+ ## Usage
10
+ Here's an example for adding the configuration to a Rails app in `config/initializers/usos_auth_lib.rb`:
11
+ ```ruby
12
+ UsosAuthLib.configure do |config|
13
+ config.api_key = "ENV.fetch('API_KEY', nil)"
14
+ config.api_secret = "ENV.fetch('API_SECRET', nil)"
15
+ config.usos_base_url = 'https://usosapps.umk.pl/'
16
+ config.scopes = 'email|grades'
17
+ config.redirect_path = '/usos_auth'
18
+ end
19
+ ```
20
+
21
+ `config.api_key` -> Consumer api key generated by USOS. <br>
22
+ `config.api_key` -> Consumer secret key generated by USOS. <br>
23
+ `config.usos_base_url` -> Your consumer secret key generated by USOS. <br>
24
+ `config.api_key` -> Your consumer secret key generated by USOS. <br>
25
+ `config.api_key` -> Your consumer secret key generated by USOS. <br>
26
+
27
+ The next step is to mount our routes for `authorize_user` and `callback` to a Rails app in `config/routes.rb`:
28
+ ```ruby
29
+ mount UsosAuthLib::Engine => '/usos_auth_lib'
30
+ get '/authorize_user', to: 'usos_auth_lib/usos#authorize_user'
31
+ get '/callback', to: 'usos_auth_lib/usos#callback'
32
+ ```
33
+
34
+ `get '/authorize_user'` -> replace it with your route or leave it, this is the route used to authorize the user via the USOS API <br>
35
+ `get '/callback'` -> replace it with your route or leave it, this is the route used to return from USOS, <span style="color: red;">THIS IS NOT THE ROUTE RETURN FROM THE LIBRARY!</span>, this route is the one we added to the configuration
36
+
37
+ Here's an example for creating a user or obtain it from our database in `models/users.rb`:
38
+
39
+ ```ruby
40
+ class User < ApplicationRecord
41
+ def self.from_usos(token)
42
+ user = User.where(email: token[:email]).first
43
+ user = User.create(
44
+ email: token[:email],
45
+ first_name: token[:first_name],
46
+ last_name: token[:last_name],
47
+ usos_id: token[:id],
48
+ img_url: token[:photo_url] || nil
49
+ ) unless user
50
+
51
+ user
52
+ end
53
+ end
54
+ ```
55
+
56
+ Here's an example for callback method in `controllers/users_controller.rb`:
57
+ ```ruby
58
+ class UsersController < ApplicationController
59
+ def usos_auth
60
+ session[:access_token] = params[:token]
61
+ session[:access_token_secret] = params[:secret]
62
+ @user = User.from_usos(params)
63
+ end
64
+ end
65
+ ```
66
+
67
+ Here's an example for use of handle_request method in `controllers/users_controller.rb`:
68
+ ```ruby
69
+ class UsersController < ApplicationController
70
+ include UsosAuthCommon
71
+ def usos_auth
72
+ session[:access_token] = params[:token]
73
+ session[:access_token_secret] = params[:secret]
74
+ @user = User.from_usos(params)
75
+ end
76
+
77
+ def grades
78
+ response = handle_request(session[:access_token], session[:access_token_secret], '/services/grades/terms2?term_ids=2023/24Z')
79
+
80
+ render json: response
81
+ end
82
+ end
83
+ ```
84
+ In this example, we want to retrieve all grades from the 2023/2024Z semester.
85
+
86
+ ## Scopes
87
+ When you request a Request Token, you may pass the scopes parameter, which describes the things you want the User to share with you. Many API methods require you to have the access to multiple scopes.
88
+
89
+ When you ask a User to authorize your Request Token, USOS API will notify the User which scopes your application requires. Choose wisely - users may discard your request if you want too much!
90
+
91
+ Currently available scope keys:
92
+
93
+ - What you get by default: Permission to read basic user information (such as user's name and ID). You don't need to request this permission explicitly - you receive it by default with each Access Token.
94
+ - cards: Provides access to user's ID cards data, such as chip uid or expiration date
95
+ - change_all_preferences: Allows you to change user preferences (via the uprefs module). You may need some other scopes in order to change or view some of the preferences. Also, the access to some important preferences may be restricted in other ways, i.e. only Administrative Consumers may be allowed to change them.
96
+ - crstests: Provides access to details and results of user's course tests.
97
+ - dorm_admin: Provides access to administrative housing operations on user's behalf. For more information, please visit the housing module.
98
+ - edit_user_attrs: Allows editing user's attributes (the same thet the user can edit on his USOSweb profile page).
99
+ - email: Provides access to user's email address.
100
+ - events: Allows access to user's preferences, push notifications, etc.
101
+ - grades: Provides access to grades information.
102
+ - grades_write: Allows access to read and write exam reports.
103
+ - mailclient: Provides access to the mailclient module (in the name of your user). Currently only a small set of methods is available for non-administrative consumers, but this set will be growing.
104
+ - mobile_numbers: Provides access to user's personal mobile phone number(s).
105
+ - offline_access: Enables your application to perform authorized requests on behalf of the user at any time. By default, Access Tokens expire after a short time period to ensure applications only make requests on behalf of users when they are actively using the application. This scope makes Access Tokens long-lived.
106
+ - other_emails: Provides access to email addresses of other users (i.e. the ones related to your user).
107
+ - payments: Allows access to your payments.
108
+ - personal: Provides access to user's personal data, such as PESEL number, date of birth, etc.
109
+ - photo: Provides read access to user's photo and his/her photo visibility preferences ("who can see my photo?").
110
+ - placement_tests: Provides access to results of user's placement tests in foreign languages.
111
+ - session_debugging_perms: (for Administrative Consumers only) Allows access to official permissions related to the user's session debugging rights. Allows you to get the answer to the question "Is my user permitted to debug the session of user X?". See "can_i_debug" field of the services/users/user method for more information.
112
+ - slips: Provides access to most of the actions within the Clearance Slips module. With this scope you can view, create and edit slips, answer questions and perform any non-administrative action which the user can perform via USOSweb. You will need an additional 'slips_admin' scope if you want to manage slip templates too.
113
+ - slips_admin: Provides access to template management of the "slips" module. That is, it allows you to create and edit questions, mark templates as obsolete etc.
114
+ - staff_perspective: If your user is a staff member, then this scope provides access to some common student-related data usually visible only to staff members, e.g. student numbers, or broader lists of students' study programmes.
115
+ - student_exams: Provides access to lists of student's exams, information on their examiners, places the exams take place etc.
116
+ student_exams_write: Allows to register and unregister the student from his exams.
117
+ - studies: Provides access to lists of programmes, courses, classes and groups which the user attends (as a student).
118
+ - surveys_filling: Allows access to surveys from students point of view. With this scope you can fetch and fill out surveys.
119
+ - surveys_reports: Allows access to reports on surveys that concern user as a lecturer.
120
+ - theses_protocols_write: Allows access to editing diploma exam protocols, e.g. signing protocols.
121
+
122
+ Source: `https://apps.usos.edu.pl/developers/api/authorization/`
123
+
124
+ ## Installation
125
+ Add this line to your application's Gemfile:
126
+
127
+ ```ruby
128
+ gem "usos_auth_lib"
129
+ ```
130
+
131
+ And then execute:
132
+ ```bash
133
+ $ bundle
134
+ ```
135
+
136
+ Or install it yourself as:
137
+ ```bash
138
+ $ gem install usos_auth_lib
139
+ ```
140
+
141
+ ## Contributing
142
+ Contribution directions go here.
143
+
144
+ ## License
145
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+
3
+ APP_RAKEFILE = File.expand_path('spec/dummy/Rakefile', __dir__)
4
+ load 'rails/tasks/engine.rake'
5
+
6
+ load 'rails/tasks/statistics.rake'
7
+
8
+ require 'bundler/gem_tasks'
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/usos_auth_lib .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,36 @@
1
+ module UsosAuthLib
2
+ class UsosController < ActionController::Base
3
+ def authorize_user
4
+ authorization_url = usos_authorizer.authorize(session, request)
5
+
6
+ redirect_to authorization_url, allow_other_host: true
7
+ end
8
+
9
+ def callback
10
+ verifier = params[:oauth_verifier]
11
+ access_token = usos_authorizer.access_token(session, verifier, nil, nil)
12
+
13
+ response = access_token.get('/services/users/user?fields=id|first_name|last_name|email|photo_urls')
14
+ parsed_response = JSON.parse(response.body)
15
+
16
+ puts parsed_response
17
+
18
+ redirect_path = UsosAuthLib.configuration.redirect_path
19
+
20
+ url = url_for(redirect_path)
21
+
22
+ url << "?id=#{parsed_response['id']}&email=#{parsed_response['email']}"
23
+ url << "&first_name=#{parsed_response['first_name']}&last_name=#{parsed_response['last_name']}"
24
+ url << "&photo_url=#{parsed_response['photo_urls']['50x50']}"
25
+ url << "&token=#{access_token.token}&secret=#{access_token.secret}"
26
+
27
+ redirect_to url, allow_other_host: true
28
+ end
29
+
30
+ private
31
+
32
+ def usos_authorizer
33
+ UsosAuthLib::UsosAuthorizer.new
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,4 @@
1
+ module UsosAuthLib
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module UsosAuthLib
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module UsosAuthLib
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,18 @@
1
+ module UsosAuthCommon
2
+ extend ActiveSupport::Concern
3
+
4
+ def handle_request(access_token, access_token_secret, service_path)
5
+ access_token = usos_authorizer.access_token(session, nil, access_token, access_token_secret)
6
+
7
+ puts access_token
8
+
9
+ response = access_token.get(service_path)
10
+ JSON.parse(response.body)
11
+ end
12
+
13
+ private
14
+
15
+ def usos_authorizer
16
+ UsosAuthLib::UsosAuthorizer.new
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ module UsosAuthLib
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Usos auth lib</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "usos_auth_lib/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ UsosAuthLib::Engine.routes.draw do
2
+ post '/authorize_user', to: 'usos#authorize_user'
3
+ post '/callback', to: 'usos#callback'
4
+ post '/handle_request', to: 'usos#handle_request'
5
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :usos_auth_lib do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,5 @@
1
+ module UsosAuthLib
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace UsosAuthLib
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module UsosAuthLib
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,82 @@
1
+ require 'usos_auth_lib/version'
2
+ require 'usos_auth_lib/engine'
3
+ require 'oauth'
4
+
5
+ module UsosAuthLib
6
+ class Configuration
7
+ attr_accessor :api_key, :api_secret, :usos_base_url, :scopes, :redirect_path
8
+
9
+ def initialize
10
+ @api_key = nil
11
+ @api_secret = nil
12
+ @usos_base_url = nil
13
+ @scopes = nil
14
+ @redirect_path = nil
15
+ end
16
+ end
17
+
18
+ class << self
19
+ attr_accessor :configuration
20
+
21
+ def configure
22
+ self.configuration ||= Configuration.new
23
+ yield(configuration)
24
+ end
25
+ end
26
+
27
+ class UsosAuthorizer
28
+ def initialize
29
+ config = UsosAuthLib.configuration
30
+ @api_key = config.api_key
31
+ @api_secret = config.api_secret
32
+ @usos_base_url = config.usos_base_url
33
+ @scopes = config.scopes
34
+ end
35
+
36
+ def authorize(session, request)
37
+ callback_url = "#{request.protocol}#{request.host_with_port}/callback"
38
+ request_token = consumer.get_request_token(
39
+ { oauth_callback: callback_url },
40
+ { scopes: @scopes }
41
+ )
42
+ session[:request_token] = request_token.token
43
+ session[:request_token_secret] = request_token.secret
44
+
45
+ request_token.authorize_url
46
+ end
47
+
48
+ def access_token(session, verifier, access_token, access_token_secret)
49
+ token = nil
50
+
51
+ if verifier.nil?
52
+ token = OAuth::AccessToken.new(
53
+ consumer,
54
+ access_token,
55
+ access_token_secret
56
+ )
57
+ else
58
+ request_token = OAuth::RequestToken.new(
59
+ consumer,
60
+ session.delete(:request_token),
61
+ session.delete(:request_token_secret)
62
+ )
63
+ token = request_token.get_access_token(oauth_verifier: verifier)
64
+ end
65
+
66
+ token
67
+ end
68
+
69
+ private
70
+
71
+ def consumer
72
+ OAuth::Consumer.new(
73
+ @api_key,
74
+ @api_secret,
75
+ site: @usos_base_url,
76
+ request_token_path: '/services/oauth/request_token',
77
+ authorize_path: '/services/oauth/authorize',
78
+ access_token_path: '/services/oauth/access_token'
79
+ )
80
+ end
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: usos_auth_lib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mikolajczu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 7.1.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 7.1.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: "\n Enhance your Ruby applications with UsosAuthLib, a robust gem
56
+ for streamlined user authentication\n via the USOS API. Simplify your workflow,
57
+ contribute to the community,\n and power up your Ruby projects effortlessly with
58
+ UsosAuthLib.\n "
59
+ email:
60
+ - mikeyczu@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - MIT-LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - app/assets/config/usos_auth_lib_manifest.js
69
+ - app/assets/stylesheets/usos_auth_lib/application.css
70
+ - app/controllers/usos_auth_lib/usos_controller.rb
71
+ - app/helpers/usos_auth_lib/application_helper.rb
72
+ - app/jobs/usos_auth_lib/application_job.rb
73
+ - app/mailers/usos_auth_lib/application_mailer.rb
74
+ - app/models/concerns/usos_auth_common.rb
75
+ - app/models/usos_auth_lib/application_record.rb
76
+ - app/views/layouts/usos_auth_lib/application.html.erb
77
+ - config/routes.rb
78
+ - lib/tasks/usos_auth_lib_tasks.rake
79
+ - lib/usos_auth_lib.rb
80
+ - lib/usos_auth_lib/engine.rb
81
+ - lib/usos_auth_lib/version.rb
82
+ homepage: https://github.com/mikolajczu/usos_auth_lib
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ homepage_uri: https://github.com/mikolajczu/usos_auth_lib
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.4.10
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: 'UsosAuthLib: Simplify user authentication in Ruby applications with this
106
+ powerful gem tailored for seamless integration with the USOS API. Streamline your
107
+ workflow and elevate your projects effortlessly.'
108
+ test_files: []