Skip to content

Commit

Permalink
Updated OAuth logic to account for versions
Browse files Browse the repository at this point in the history
  • Loading branch information
louispt1 committed Dec 20, 2024
1 parent fa7c6c5 commit ff468a7
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 28 deletions.
10 changes: 8 additions & 2 deletions app/controllers/api/v1/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class BaseController < ActionController::API
include ActionController::MimeResponds

before_action :authenticate_request!
before_action :set_version_tag

rescue_from ActionController::ParameterMissing do |e|
render json: { errors: [ e.message ] }, status: :bad_request
Expand Down Expand Up @@ -35,7 +36,6 @@ class BaseController < ActionController::API

private


def decoded_token
return @decoded_token if defined?(@decoded_token)

Expand Down Expand Up @@ -117,7 +117,13 @@ def require_user
false
end

private
def set_version_tag
@version_tag = params[:version] || Version::DEFAULT_TAG
end

def current_version_tag
@version_tag
end

def find_user_from_token
return unless decoded_token
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/saved_scenarios_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def saved_scenario_params
end

def engine_client
MyEtm::Auth.engine_client(current_user)#, scopes: doorkeeper_token.scopes)
MyEtm::Auth.engine_client(current_user, current_version_tag)
end
end
end
Expand Down
13 changes: 12 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class ApplicationController < ActionController::Base
before_action :configure_sentry
before_action :store_user_location!, if: :storable_location?
before_action :store_redirect_url
before_action :set_version_tag

helper_method :current_version_tag

rescue_from CanCan::AccessDenied do |_exception|
if current_user
Expand Down Expand Up @@ -75,7 +78,7 @@ def configure_sentry
end

def engine_client
MyEtm::Auth.engine_client(current_user)
MyEtm::Auth.engine_client(current_user, current_version_tag)
end

# Internal: Renders a 404 page.
Expand Down Expand Up @@ -141,4 +144,12 @@ def store_redirect_url
session[:redirect_url] = params[:redirect_url]
end
end

def set_version_tag
@version_tag = params[:version] || Version::DEFAULT_TAG
end

def current_version_tag
@version_tag
end
end
2 changes: 1 addition & 1 deletion app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def stats_for_destroy
personal_access_tokens: current_user.personal_access_tokens.not_expired.count,
oauth_applications: current_user.oauth_applications.count,
collections: 0
# collections: current_user.collections.count
# collections: current_user.collections.count # TODO: Re-Implement
}
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def format_staff_config(config, app)
etengine_url = Settings.etengine.uri || "http://YOUR_ETENGINE_URL"

format(config, app.attributes.symbolize_keys.merge(
myetm_url: root_url.chomp("/root"),
myetm_url: root_url.chomp("/"),
etengine_url: etengine_url,
etmodel_url: Settings.etmodel.uri || "http://YOUR_ETMODEL_URL",
collections_url: Settings.collections.uri || "http://YOUR_COLLECTIONS_URL",
etengine_uid: Doorkeeper::Application.find_by(uri: etengine_url)&.uid || "YOUR_ETEngine_ID_HERE"
etengine_uid: Doorkeeper::Application.find_by(uri: etengine_url)&.uid || "YOUR_ETEngine_ID"
))
end

Expand Down
41 changes: 31 additions & 10 deletions app/models/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
# A valid version of the ETM
class Version
URL = "energytransitionmodel.com".freeze
DEFAULT_TAG = Rails.env.development? ? "local" : "latest" # Default to "local" in development

# Tag => prefix
LIST = {
"local" => "",
"latest" => "",
"stable.01" => "stable.",
"stable.02" => "stable2."
}.freeze

LOCAL_URLS = {
"collections" => Settings.collections.uri,
"model" => Settings.etmodel.uri,
"engine" => Settings.etengine.uri
}.freeze

# All available versions. Uses ActiveRecord syntax 'all' to
# make future porting to db easier
def self.all
Expand All @@ -21,26 +29,39 @@ def self.tags
LIST.keys
end

def self.model_url(tag)
"https://#{LIST[tag]}#{Version::URL}"
def self.collections_url(tag = nil)
build_url("collections", tag)
end

def self.engine_url(tag)
"https://#{LIST[tag]}engine.#{Version::URL}"
def self.model_url(tag = nil)
build_url("model", tag)
end

# TODO: Collections url

# TODO: urls for local development => Add a local version and
# exceptions for the urls
def self.engine_url(tag = nil)
build_url("engine", tag)
end

def self.as_json(*)
Version.tags.map do |tag|
{
tag: tag,
url: Version.model_url(tag),
engine_url: Version.engine_url(tag)
model_url: model_url(tag),
engine_url: engine_url(tag),
collections_url: collections_url(tag)
}
end
end

private

def self.build_url(context, tag)
tag ||= DEFAULT_TAG
raise ArgumentError, "Invalid version tag: #{tag}" unless LIST.key?(tag)

if tag == "local"
LOCAL_URLS[context]
else
"https://#{LIST[tag]}#{context == 'model' ? '' : "#{context}."}#{URL}"
end
end
end
2 changes: 1 addition & 1 deletion app/services/create_staff_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.call(user, app_config, uri: nil)

# Update application attributes
app.attributes = app_config.to_model_attributes.merge(
owner: user,
owner_id: user.id,
uri: parsed_uri.to_s,
redirect_uri: redirect_uri.to_s
)
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_sidebar.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.sidebar.fixed.top-0.bottom-0.overflow-y-auto.bg-midnight-300{class: 'lg:left-0 w-[300px]'}
-# Where does the logo refer to? REMEMBER LAST VERSION IN COOKIE
%a.logo.p-5.mb-3.inline-block.w-full.text-midnight-800
%a.logo.p-5.mb-3.inline-block.w-full.text-midnight-800{href: Version.model_url(current_version_tag || "latest")}
= image_tag 'header/logo-round.png', class: 'h-8 inline mb-1 mr-2 hover:animate-spin'
%span Energy Transition Model

Expand Down
2 changes: 1 addition & 1 deletion config/initializers/doorkeeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#
# access_token_class "Doorkeeper::AccessToken"
# access_grant_class "Doorkeeper::AccessGrant"
application_class 'OAuthApplication'
application_class "OAuthApplication"
#
# Don't forget to include Doorkeeper ORM mixins into your custom models:
#
Expand Down
14 changes: 7 additions & 7 deletions lib/myetm/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ def client_app_client(user, client_app)
end
end

# TODO: use the uri's generated by Version to dynamiclly pick the correct
# model and engine
def engine_client(user)
engine = OAuthApplication.find_by(uri: Settings.etengine.uri)
# TODO: Refactor usage to include the version_tag
def engine_client(user, version_tag = Version::DEFAULT_TAG)
uri = Version.engine_url(version_tag)
engine = OAuthApplication.find_by(uri: uri)
client_app_client(user, engine)
end

def model_client(user)
model = OAuthApplication.find_by(uri: Settings.etmodel.uri)
def model_client(user, version_tag = Version::DEFAULT_TAG)
uri = Version.model_url(version_tag)
model = OAuthApplication.find_by(uri: uri)
client_app_client(user, model)
end


# Checks if the token is in JWT format
def jwt_format?(token)
token.count(".") == 2
Expand Down
3 changes: 2 additions & 1 deletion spec/controllers/api/v1/versions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

expect(parsed_response['versions']).to be_present
expect(parsed_response['versions'].map { |v| v['tag'] }).to match_array(Version.tags)
expect(parsed_response['versions'].first).to have_key('url')
expect(parsed_response['versions'].first).to have_key('model_url')
expect(parsed_response['versions'].first).to have_key('engine_url')
expect(parsed_response['versions'].first).to have_key('collections_url')
end
end
end

0 comments on commit ff468a7

Please sign in to comment.