From 2f55c33f150e5dec6840c3a0bf083c6a6bbceae4 Mon Sep 17 00:00:00 2001 From: Benedikt Hensen Date: Sun, 1 Sep 2024 20:07:31 +0200 Subject: [PATCH] added method of directly setting the access token in the OIDC service --- .../Scripts/OpenIDConnectService.cs | 21 +++++++++++++++- .../OpenIDConnectServiceTests.cs | 25 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Assets/i5 Toolkit for Unity/Runtime/OpenID Connect/Scripts/OpenIDConnectService.cs b/Assets/i5 Toolkit for Unity/Runtime/OpenID Connect/Scripts/OpenIDConnectService.cs index 47d12998..155409be 100644 --- a/Assets/i5 Toolkit for Unity/Runtime/OpenID Connect/Scripts/OpenIDConnectService.cs +++ b/Assets/i5 Toolkit for Unity/Runtime/OpenID Connect/Scripts/OpenIDConnectService.cs @@ -28,7 +28,7 @@ public class OpenIDConnectService : IUpdateableService /// The access token of the logged in user /// Use this token to access data about the user or to access protected Web resources /// - public string AccessToken { get; set; } + public string AccessToken { get; private set; } /// /// Is true if the user of the application is currently logged in @@ -283,5 +283,24 @@ public async void Update() i5Debug.LogError("An error occurred during the login process. The access token is empty.", this); } } + + /// + /// Directly sets the access token and triggers the login event + /// Can be used to log in a user again with the same access token after initially logging out + /// + /// + /// + public void LoginWithAccessToken(string accessToken) + { + AccessToken = accessToken; + if (!string.IsNullOrEmpty(AccessToken)) + { + LoginCompleted?.Invoke(this, EventArgs.Empty); + } + else + { + i5Debug.LogError("An empty access token was passed to the service.", this); + } + } } } diff --git a/Assets/i5 Toolkit for Unity/Tests/Editor/OpenID Connect/OpenIDConnectServiceTests.cs b/Assets/i5 Toolkit for Unity/Tests/Editor/OpenID Connect/OpenIDConnectServiceTests.cs index b0761e92..f6ad3c66 100644 --- a/Assets/i5 Toolkit for Unity/Tests/Editor/OpenID Connect/OpenIDConnectServiceTests.cs +++ b/Assets/i5 Toolkit for Unity/Tests/Editor/OpenID Connect/OpenIDConnectServiceTests.cs @@ -390,5 +390,28 @@ public IEnumerator IsLoggedIn_SuccessfulLogout_ReturnsFalse() Assert.IsFalse(oidc.IsLoggedIn); } - } + + [Test] + public void LoginWithAccessToken_LoginCompletedEventRaised() + { + OpenIDConnectService oidc = new OpenIDConnectService(); + int events = 0; + oidc.LoginCompleted += delegate + { + events++; + }; + + oidc.LoginWithAccessToken("myAccessToken"); + Assert.AreEqual(1, events); + } + + [Test] + public void LoginWithAccessToken_AccessTokenSet() + { + OpenIDConnectService oidc = new OpenIDConnectService(); + string accessToken = "myAccessToken"; + oidc.LoginWithAccessToken(accessToken); + Assert.AreEqual(accessToken, oidc.AccessToken); + } + } }