aha_builder_core 1.0.0 → 1.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ff589bfc8b84ca502361ec7db8fcd9052a02fcef786d45bdfa8f1463ab2ce92
4
- data.tar.gz: 4271e05f6300fda500e3062d1ae8f2ad3ab0227cff5925919d8fd2a97c4b49c1
3
+ metadata.gz: cec0f75d63cf8393eb2c6130cefa3ff8f06c041536a2fc8062ca229c13647697
4
+ data.tar.gz: a8759f979fe6256c4627fe0bca73c4fe2dca1e0ca1f6874b03b145bfa46241f0
5
5
  SHA512:
6
- metadata.gz: 3698b01b1e05371f68dd84881ac9ebda989a629def95533db97c6f48bf97803095a43ae7c6c2af6fcf6e5fe5f5700826da0a54780dccbba85860c25d92db12e8
7
- data.tar.gz: 227dafe8165fc4562d26761a331bd4f128ec0ba10bdf1a2a8c859f15ee71c8ac95090f457bc772b3db9bede007f253523558294332b34fdd6acfe2fab4d96fd5
6
+ metadata.gz: f4e44c3f8cf0c1eb89f179ad97562e913f86e4621eba409b7868ff1cf26f24aff42810951430ea0e88a1e22c61aac7991baf9c37fc38472c1f2d6b34ef384220
7
+ data.tar.gz: 39a1ea75024a765fa7f74ed6b223d0de67d3aa5fb336dad99d644f42516cd8a3514e81535f7ebf0a4c4fe07395c3239bbea2b3f2f8c39522929879d0729c7f75
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Aha
4
4
  module Auth
5
- VERSION = "1.0.0"
5
+ VERSION = "1.0.1"
6
6
  end
7
7
  end
@@ -0,0 +1,21 @@
1
+ Description:
2
+ Generates authentication setup for Aha! Builder Core client
3
+
4
+ This generator creates:
5
+ - SessionsController with new and callback actions for authentication flow
6
+ - User model for storing authenticated user information
7
+ - Current model for thread-isolated current user access
8
+ - Authentication concern to be included in ApplicationController
9
+
10
+ Example:
11
+ bin/rails generate aha_builder_core:authentication
12
+
13
+ This will create:
14
+ app/controllers/sessions_controller.rb
15
+ app/controllers/concerns/authentication.rb
16
+ app/models/user.rb
17
+ app/models/current.rb
18
+
19
+ And add routes:
20
+ get "login", to: "sessions#new", as: :new_session
21
+ get "callback", to: "sessions#callback", as: :session_callback
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+
5
+ module AhaBuilderCore
6
+ module Generators
7
+ class AuthenticationGenerator < Rails::Generators::Base
8
+ source_root File.expand_path("templates", __dir__)
9
+
10
+ def create_sessions_controller
11
+ template "sessions_controller.rb.erb", "app/controllers/sessions_controller.rb"
12
+ end
13
+
14
+ def create_user_model
15
+ template "user.rb.erb", "app/models/user.rb"
16
+ end
17
+
18
+ def create_current_model
19
+ template "current.rb.erb", "app/models/current.rb"
20
+ end
21
+
22
+ def create_authentication_concern
23
+ template "authentication.rb.erb", "app/controllers/concerns/authentication.rb"
24
+ end
25
+
26
+ def add_routes
27
+ route <<~RUBY
28
+ get "login", to: "sessions#new", as: :new_session
29
+ get "callback", to: "sessions#callback", as: :session_callback
30
+ RUBY
31
+ end
32
+
33
+ def display_instructions
34
+ say "\nAuthentication setup complete!", :green
35
+ say "\nNext steps:"
36
+ say "1. Configure your environment variables:"
37
+ say " - APPLICATION_URL: Your application's base URL"
38
+ say " - AHA_AUTH_SERVER_URL: The authentication server URL"
39
+ say " - AHA_AUTH_CLIENT_ID: Your client ID"
40
+ say " - AHA_AUTH_API_KEY: Your API key (if using server-to-server operations)"
41
+ say "\n2. Run migrations to create the users table:"
42
+ say " rails generate migration CreateUsers auth_identifier:string:index email:string first_name:string last_name:string email_verified:boolean"
43
+ say " rails db:migrate"
44
+ say "\n3. Include authentication in ApplicationController:"
45
+ say " include Authentication"
46
+ say "\n4. Protect routes with:"
47
+ say " before_action :authenticate"
48
+ end
49
+
50
+ private
51
+
52
+ def application_name
53
+ Rails.application.class.module_parent.name
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,48 @@
1
+ module Authentication
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_action :authenticate
6
+ helper_method :current_user, :logged_in?
7
+ end
8
+
9
+ private
10
+
11
+ def authenticate
12
+ if session[:session_token].present?
13
+ session_result = Aha::Auth.validate_session(
14
+ session[:session_token],
15
+ refresh_token: session[:refresh_token]
16
+ )
17
+
18
+ if session_result.valid?
19
+ # If tokens were refreshed, update stored tokens
20
+ if session_result.refreshed?
21
+ session[:session_token] = session_result.new_session_token
22
+ session[:refresh_token] = session_result.new_refresh_token
23
+ end
24
+
25
+ # Set current user
26
+ @current_user = User.find_by(id: session[:user_id])
27
+ Current.user = @current_user
28
+ else
29
+ reset_session
30
+ redirect_to new_session_path, alert: "Your session has expired. Please log in again."
31
+ end
32
+ else
33
+ redirect_to new_session_path
34
+ end
35
+ rescue Aha::Auth::ApiError => e
36
+ Rails.logger.error "Session validation failed: #{e.message}"
37
+ reset_session
38
+ redirect_to new_session_path, alert: "Authentication error. Please log in again."
39
+ end
40
+
41
+ def current_user
42
+ @current_user ||= Current.user
43
+ end
44
+
45
+ def logged_in?
46
+ current_user.present?
47
+ end
48
+ end
@@ -0,0 +1,4 @@
1
+ # Provides a thread-isolated attributes singleton for current user
2
+ class Current < ActiveSupport::CurrentAttributes
3
+ attribute :user
4
+ end
@@ -0,0 +1,44 @@
1
+ class SessionsController < ApplicationController
2
+ skip_before_action :authenticate, only: [:new, :callback]
3
+
4
+ def new
5
+ # Start authentication by redirecting to the auth URL
6
+ redirect_to Aha::Auth.login_url(
7
+ state: { return_to: params[:return_to] || root_path }.to_json,
8
+ redirect_uri: "#{ENV['APPLICATION_URL']}/callback"
9
+ ), allow_other_host: true
10
+ end
11
+
12
+ def callback
13
+ # Handle the callback from the authentication server
14
+ if params[:code].present?
15
+ result = Aha::Auth.authenticate_with_code(code: params[:code])
16
+
17
+ # Find or create a local user record linked to Builder Core user
18
+ user = User.find_or_initialize_by(auth_identifier: result[:user]["id"])
19
+
20
+ # Update local user attributes
21
+ user.update!(
22
+ email: result[:user]["email"],
23
+ first_name: result[:user]["first_name"],
24
+ last_name: result[:user]["last_name"],
25
+ email_verified: result[:user]["email_verified"]
26
+ )
27
+
28
+ # Store tokens in session
29
+ session[:session_token] = result[:session_token]
30
+ session[:refresh_token] = result[:refresh_token]
31
+ session[:user_id] = user.id
32
+
33
+ # Parse state to get return_to path
34
+ state = JSON.parse(params[:state]) rescue {}
35
+ redirect_to state["return_to"] || root_path
36
+ else
37
+ # Authentication failed or was cancelled
38
+ redirect_to new_session_path, alert: "Authentication failed. Please try again."
39
+ end
40
+ rescue Aha::Auth::ApiError => e
41
+ Rails.logger.error "Authentication failed: #{e.message}"
42
+ redirect_to new_session_path, alert: "Authentication failed. Please try again."
43
+ end
44
+ end
@@ -0,0 +1,8 @@
1
+ class User < ApplicationRecord
2
+ validates :auth_identifier, presence: true, uniqueness: true
3
+ validates :email, presence: true, uniqueness: true
4
+
5
+ def name
6
+ "#{first_name} #{last_name}".strip.presence || email
7
+ end
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aha_builder_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aha! Labs Inc.
@@ -111,6 +111,12 @@ files:
111
111
  - lib/aha/auth/users_resource.rb
112
112
  - lib/aha/auth/version.rb
113
113
  - lib/aha_builder_core.rb
114
+ - lib/generators/aha_builder_core/authentication/USAGE
115
+ - lib/generators/aha_builder_core/authentication/authentication_generator.rb
116
+ - lib/generators/aha_builder_core/authentication/templates/authentication.rb.erb
117
+ - lib/generators/aha_builder_core/authentication/templates/current.rb.erb
118
+ - lib/generators/aha_builder_core/authentication/templates/sessions_controller.rb.erb
119
+ - lib/generators/aha_builder_core/authentication/templates/user.rb.erb
114
120
  homepage: https://www.aha.io
115
121
  licenses:
116
122
  - MIT
@@ -124,7 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
124
130
  requirements:
125
131
  - - ">="
126
132
  - !ruby/object:Gem::Version
127
- version: 3.3.0
133
+ version: 3.2.0
128
134
  required_rubygems_version: !ruby/object:Gem::Requirement
129
135
  requirements:
130
136
  - - ">="