diff --git a/apps/authentication/models.py b/apps/authentication/models.py index c0713ac1a..d04a3fa4e 100644 --- a/apps/authentication/models.py +++ b/apps/authentication/models.py @@ -114,6 +114,14 @@ class ConnectionToken(OrgModelMixin, models.JMSModel): def is_expired(self): return self.date_expired < timezone.now() + @property + def expire_time(self): + interval = self.date_expired - timezone.now() + seconds = interval.total_seconds() + if seconds < 0: + seconds = 0 + return int(seconds) + def expire(self): self.date_expired = timezone.now() self.save() diff --git a/apps/authentication/serializers/connection_token.py b/apps/authentication/serializers/connection_token.py index c2af89aff..1b639bec6 100644 --- a/apps/authentication/serializers/connection_token.py +++ b/apps/authentication/serializers/connection_token.py @@ -20,7 +20,8 @@ __all__ = [ class ConnectionTokenSerializer(OrgResourceModelSerializerMixin): type_display = serializers.ReadOnlyField(source='get_type_display', label=_("Type display")) - validity = serializers.BooleanField(source='is_valid', read_only=True, label=_('Validity')) + is_valid = serializers.BooleanField(read_only=True, label=_('Validity')) + expire_time = serializers.IntegerField(read_only=True, label=_('Expired time')) class Meta: model = ConnectionToken @@ -35,7 +36,7 @@ class ConnectionTokenSerializer(OrgResourceModelSerializerMixin): ] read_only_fields = [ # 普通 Token 不支持指定 user - 'user', 'validity', + 'user', 'is_valid', 'expire_time', 'type_display', 'user_display', 'system_user_display', 'asset_display', 'application_display', ] @@ -184,6 +185,7 @@ class ConnectionTokenSecretSerializer(OrgResourceModelSerializerMixin): domain = ConnectionTokenDomainSerializer(read_only=True) cmd_filter_rules = ConnectionTokenCmdFilterRuleSerializer(many=True) actions = ActionsField() + expired_at = serializers.IntegerField() class Meta: model = ConnectionToken