From ce22adf8119e76d468008a1cb3204bd0679b5288 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Fri, 13 Sep 2024 16:47:31 +0200 Subject: [PATCH 1/3] [#10798] Indicate in manager if api_entreprise_token is present / valid / expired --- app/dashboards/procedure_dashboard.rb | 2 ++ app/fields/jwt_field.rb | 22 ++++++++++++++++++++++ app/views/fields/jwt_field/_show.html.erb | 1 + 3 files changed, 25 insertions(+) create mode 100644 app/fields/jwt_field.rb create mode 100644 app/views/fields/jwt_field/_show.html.erb diff --git a/app/dashboards/procedure_dashboard.rb b/app/dashboards/procedure_dashboard.rb index 79f77b233..8d9444e94 100644 --- a/app/dashboards/procedure_dashboard.rb +++ b/app/dashboards/procedure_dashboard.rb @@ -29,6 +29,7 @@ class ProcedureDashboard < Administrate::BaseDashboard updated_at: Field::DateTime, for_individual: Field::Boolean, auto_archive_on: Field::DateTime, + api_entreprise_token: JwtField, published_at: Field::DateTime, unpublished_at: Field::DateTime, hidden_at: Field::DateTime, @@ -100,6 +101,7 @@ class ProcedureDashboard < Administrate::BaseDashboard :published_types_de_champ_public, :published_types_de_champ_private, :for_individual, + :api_entreprise_token, :auto_archive_on, :passer_en_construction_email_template, :passer_en_instruction_email_template, diff --git a/app/fields/jwt_field.rb b/app/fields/jwt_field.rb new file mode 100644 index 000000000..44b36d7e6 --- /dev/null +++ b/app/fields/jwt_field.rb @@ -0,0 +1,22 @@ +require "administrate/field/base" + +class JwtField < Administrate::Field::Base + def to_s + if data.present? + begin + decoded_token = JWT.decode(data, nil, false) + expiration = Time.zone.at(decoded_token[0]['exp']) + + if expiration < Time.zone.now + "Token présent, expiré le #{expiration.strftime('%d/%m/%Y à %H:%M')}" + else + "Token présent, expirera le #{expiration.strftime('%d/%m/%Y à %H:%M')}" + end + rescue JWT::DecodeError => e + "Token invalide : #{e.message}" + end + else + "Pas de token" + end + end +end diff --git a/app/views/fields/jwt_field/_show.html.erb b/app/views/fields/jwt_field/_show.html.erb new file mode 100644 index 000000000..6d9dbc907 --- /dev/null +++ b/app/views/fields/jwt_field/_show.html.erb @@ -0,0 +1 @@ +<%= field.to_s %> From 539853255db4c5a418f8a62d2793551ebd246eca Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Fri, 13 Sep 2024 16:52:42 +0200 Subject: [PATCH 2/3] Missing frozen_string_literal --- app/fields/jwt_field.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/fields/jwt_field.rb b/app/fields/jwt_field.rb index 44b36d7e6..188ca2f08 100644 --- a/app/fields/jwt_field.rb +++ b/app/fields/jwt_field.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "administrate/field/base" class JwtField < Administrate::Field::Base From 7ace67001083395a3d297bc62f3d06324119bb28 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 17 Sep 2024 09:47:46 +0200 Subject: [PATCH 3/3] [#10798] fix tests --- app/fields/jwt_field.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/fields/jwt_field.rb b/app/fields/jwt_field.rb index 188ca2f08..e2c944066 100644 --- a/app/fields/jwt_field.rb +++ b/app/fields/jwt_field.rb @@ -7,7 +7,8 @@ class JwtField < Administrate::Field::Base if data.present? begin decoded_token = JWT.decode(data, nil, false) - expiration = Time.zone.at(decoded_token[0]['exp']) + + return "Token présent, sans expiration" unless decoded_token[0].key?('exp') if expiration < Time.zone.now "Token présent, expiré le #{expiration.strftime('%d/%m/%Y à %H:%M')}"