authentication-unity icon indicating copy to clipboard operation
authentication-unity copied to clipboard

Exposing userInfoUrl and supporting deserialisation through IUserInfoProvider

Open mvriel opened this issue 2 years ago • 1 comments

For my work to support WebGL as an alternate path to the code that you have written, I need to expose the userInfoUrl to my jslib, and do my own deserialisation because I am unable to use the GetUserInfoAsync method.

Because of this, I want to ask you if you would be OK with adding the userInfoUrl property to IUserInfoProvider, and a method public IUserInfo DeserializeUserInfo(string json). The latter would just do JSONConvert, but it would keep which IUserInfo class to use internally to the *Auth classes; at the moment I need a switch statement to cast objects to the right type.

I can make a PR for you, but I though I'd ask first

mvriel avatar May 02 '23 14:05 mvriel

As an example why I would want this:

        private IUserInfo DeserializeUserInfo(string json)
        {
            if (authorizationCodeFlow is IUserInfoProviderExtra userInfoProvider)
            {
                return userInfoProvider.DeserializeUserInfo(json);
            }

            IUserInfo userInfo = identityProvider switch
            {
                IdentityProvider.Google => JsonUtility.FromJson<GoogleUserInfo>(json),
                IdentityProvider.Facebook => JsonUtility.FromJson<FacebookUserInfo>(json),
                IdentityProvider.Github => JsonUtility.FromJson<GitHubUserInfo>(json),
                _ => throw new ArgumentOutOfRangeException()
            };
            return userInfo;
        }

        private string GetUserInfoEndpoint()
        {
            if (authorizationCodeFlow is IUserInfoProviderExtra userInfoProvider)
            {
                return userInfoProvider.userInfoUrl;
            }

            return identityProvider switch
            {
                IdentityProvider.Google => ((GoogleAuth)authorizationCodeFlow).userInfoUrl,
                IdentityProvider.Facebook => ((FacebookAuth)authorizationCodeFlow).userInfoUrl,
                IdentityProvider.Github => ((GitHubAuth)authorizationCodeFlow).userInfoUrl,
                _ => throw new ArgumentOutOfRangeException()
            };
        }

In this example, I have introduced a new Interface (IUserInfoProviderExtra) for our own AuthorizationCodeFlow classes (for Azure in this case), and because of these two added fields I can get the UserInfo object and the endpoint without casting and exposing the knowledge which should be addressed how to the outer classes

mvriel avatar May 02 '23 14:05 mvriel