16678: Sets token expiration at login time. Disabled by default.
authorLucas Di Pentima <lucas@di-pentima.com.ar>
Tue, 18 Aug 2020 19:23:38 +0000 (16:23 -0300)
committerLucas Di Pentima <lucas@di-pentima.com.ar>
Tue, 18 Aug 2020 19:23:38 +0000 (16:23 -0300)
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>

lib/config/config.default.yml
lib/config/generated_config.go
services/api/app/controllers/user_sessions_controller.rb
services/api/test/functional/user_sessions_controller_test.rb

index 24e5b71c6c240c09a1eb7f2c1c397b37d34a9105..91cd8b435210f8b7debcbb718f6ea4e805b5ee5f 100644 (file)
@@ -699,8 +699,9 @@ Clusters:
       RemoteTokenRefresh: 5m
 
       # How long a client token created from a login flow will be valid without
-      # asking the user to re-login.
-      TokenLifetime: 12h
+      # asking the user to re-login. Example values: 60m, 8h.
+      # Default value zero means tokens don't have expiration.
+      TokenLifetime: 0s
 
     Git:
       # Path to git or gitolite-shell executable. Each authenticated
index e35318ff95a8a2c06bc666414bf3e875d56982ba..a2ff94c3851d5db2156a8cec72e4779abd735213 100644 (file)
@@ -705,8 +705,9 @@ Clusters:
       RemoteTokenRefresh: 5m
 
       # How long a client token created from a login flow will be valid without
-      # asking the user to re-login.
-      TokenLifetime: 12h
+      # asking the user to re-login. Example values: 60m, 8h.
+      # Default value zero means tokens don't have expiration.
+      TokenLifetime: 0s
 
     Git:
       # Path to git or gitolite-shell executable. Each authenticated
index 582b98cf2dc9d9e20b88cf0180b7a9db19fbfd8f..8e3c3ac5e3d8b8656d587e86626f86f57c33b045 100644 (file)
@@ -147,10 +147,15 @@ class UserSessionsController < ApplicationController
         find_or_create_by(url_prefix: api_client_url_prefix)
     end
 
+    token_expiration = nil
+    if Rails.configuration.Login.TokenLifetime > 0
+      token_expiration = Time.now + Rails.configuration.Login.TokenLifetime
+    end
     @api_client_auth = ApiClientAuthorization.
       new(user: user,
           api_client: @api_client,
           created_by_ip_address: remote_ip,
+          expires_at: token_expiration,
           scopes: ["all"])
     @api_client_auth.save!
 
index fc9475692a5933c2ed01f77e7871f4fd3942d7ec..cd475dea4d1849f6d99374fa3976068767ef1fcb 100644 (file)
@@ -14,7 +14,6 @@ class UserSessionsControllerTest < ActionController::TestCase
     assert_nil assigns(:api_client)
   end
 
-
   test "send token when user is already logged in" do
     authorize_with :inactive
     api_client_page = 'http://client.example.com/home'
@@ -26,6 +25,28 @@ class UserSessionsControllerTest < ActionController::TestCase
     assert_not_nil assigns(:api_client)
   end
 
+  test "login creates token without expiration by default" do
+    assert_equal Rails.configuration.Login.TokenLifetime, 0
+    authorize_with :inactive
+    api_client_page = 'http://client.example.com/home'
+    get :login, params: {return_to: api_client_page}
+    assert_not_nil assigns(:api_client)
+    assert_nil assigns(:api_client_auth).expires_at
+  end
+
+  test "login creates token with configured lifetime" do
+    token_lifetime = 1.hour
+    Rails.configuration.Login.TokenLifetime = token_lifetime
+    authorize_with :inactive
+    api_client_page = 'http://client.example.com/home'
+    get :login, params: {return_to: api_client_page}
+    assert_not_nil assigns(:api_client)
+    api_client_auth = assigns(:api_client_auth)
+    assert_in_delta(api_client_auth.expires_at,
+                    api_client_auth.updated_at + token_lifetime,
+                    1.second)
+  end
+
   test "login with remote param returns a salted token" do
     authorize_with :inactive
     api_client_page = 'http://client.example.com/home'