Skip to content

Commit

Permalink
added method of directly setting the access token in the OIDC service
Browse files Browse the repository at this point in the history
  • Loading branch information
BenediktHensen committed Sep 1, 2024
1 parent fe9fce4 commit 2f55c33
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// </summary>
public string AccessToken { get; set; }
public string AccessToken { get; private set; }

/// <summary>
/// Is true if the user of the application is currently logged in
Expand Down Expand Up @@ -283,5 +283,24 @@ public async void Update()
i5Debug.LogError("An error occurred during the login process. The access token is empty.", this);
}
}

/// <summary>
/// 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
/// </summary>
/// <param name="accessToken"></param>
/// <returns></returns>
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 2f55c33

Please sign in to comment.