Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public interface IOidcProvider
/// <returns>Returns the access token if it could be retrieved; otherwise it returns an empty string</returns>
Task<string> GetAccessTokenFromCodeAsync(string code, string redirectUri);

/// <summary>
/// Gets the complete authorization answer from the selected provider
/// </summary>
/// <param name="code">The authorization code</param>
/// <param name="redirectUri">The redirect URI which was used during the login</param>
/// <returns>Returns the complete authorization answer</returns>
Task<AbstractAuthorizationFlowAnswer> GetAuthorizationAnswerAsync(string code, string redirectUri);

/// <summary>
/// Gets the access token from a list of parameters in a Web answer
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,45 @@ public virtual async Task<string> GetAccessTokenFromCodeAsync(string code, strin
}
}

public virtual async Task<AbstractAuthorizationFlowAnswer> GetAuthorizationAnswerAsync(string code, string redirectUri)
{
if (ClientData == null)
{
i5Debug.LogError("No client data supplied for the OpenID Connect Client.\n" +
"Initialize this provider with an OpenID Connect Data file.", this);
return null;
}

WWWForm form = new WWWForm();
form.AddField("client_id", ClientData.ClientId);
form.AddField("client_secret", ClientData.ClientSecret);
form.AddField("grant_type", "authorization_code");
form.AddField("redirect_uri", redirectUri);
form.AddField("code", code);

Dictionary<string, string> headers = new Dictionary<string, string>()
{
{ "Content-Type", "application/x-www-form-urlencoded" }
};
WebResponse<string> response = await RestConnector.PostAsync(tokenEndpoint, form.data, headers);
if (response.Successful)
{
AbstractAuthorizationFlowAnswer answer =
JsonSerializer.FromJson<AbstractAuthorizationFlowAnswer>(response.Content);
if (answer == null)
{
i5Debug.LogError("Could not parse access token in code flow answer", this);
return null;
}
return answer;
}
else
{
Debug.LogError(response.ErrorMessage + ": " + response.Content);
return null;
}
}

/// <summary>
/// Gets the access token from a list of parameters in a Web answer
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,38 @@ public override async Task<string> GetAccessTokenFromCodeAsync(string code, stri
}
}

public override async Task<AbstractAuthorizationFlowAnswer> GetAuthorizationAnswerAsync(string code, string redirectUri)
{
if (ClientData == null)
{
i5Debug.LogError("No client data supplied for the OpenID Connect Client.\n" +
"Initialize this provider with an OpenID Connect Data file.", this);
return null;
}

string uri = tokenEndpoint + $"?client_id={ClientData.ClientId}" +
$"&redirect_uri={redirectUri}" + $"&client_secret={ClientData.ClientSecret}&code={code}&grant_type=authorization_code";
WebResponse<string> response = await RestConnector.PostAsync(uri, "");

if (response.Successful)
{
string response_content = response.Content;
GitHubAuthorizationFlowAnswer answer =
JsonSerializer.FromJson<GitHubAuthorizationFlowAnswer>(response_content);
if (answer == null)
{
i5Debug.LogError("Could not parse access token in code flow answer", this);
return null;
}
return answer;
}
else
{
i5Debug.LogError(response.ErrorMessage + ": " + response.Content, this);
return null;
}
}

/// <summary>
/// Gets information about the logged in user from the GitHub provider
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ public void GenerateCSRFToken()
/// <returns>Returns the access token if it could be retrieved; otherwise it returns an empty string</returns>
public override async Task<string> GetAccessTokenFromCodeAsync(string code, string redirectUri)
{
redirectUri += "code?";

EndpointsData endpoints = await InitializeEndpointsAsync();
await InitializeEndpointsAsync();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to remove the "code?" since it was giving me strange errors. Also, endpoints variable was not being used so I just removed it.

if (ClientData == null)
{
i5Debug.LogError("No client data supplied for the OpenID Connect Client.\n" +
Expand Down Expand Up @@ -100,6 +98,46 @@ public override async Task<string> GetAccessTokenFromCodeAsync(string code, stri
}
}

public override async Task<AbstractAuthorizationFlowAnswer> GetAuthorizationAnswerAsync(string code, string redirectUri)
{
await InitializeEndpointsAsync();
if (ClientData == null)
{
i5Debug.LogError("No client data supplied for the OpenID Connect Client.\n" +
"Initialize this provider with an OpenID Connect Data file.", this);
return null;
}

WWWForm form = new WWWForm();
form.AddField("code", code);
form.AddField("client_id", ClientData.ClientId);
form.AddField("client_secret", ClientData.ClientSecret);
form.AddField("redirect_uri", redirectUri);
form.AddField("grant_type", "authorization_code");

Dictionary<string, string> headers = new Dictionary<string, string>()
{
{ "Content-Type", "application/x-www-form-urlencoded" }
};
WebResponse<string> response = await RestConnector.PostAsync(tokenEndpoint, form.data, headers);
if (response.Successful)
{
GoogleAuthorizationFlowAnswer answer =
JsonSerializer.FromJson<GoogleAuthorizationFlowAnswer>(response.Content);
if (answer == null)
{
i5Debug.LogError("Could not parse access token in code flow answer", this);
return null;
}
return answer;
}
else
{
i5Debug.LogError(response.ErrorMessage + ": " + response.Content, this);
return null;
}
}

/// <summary>
/// Extracts the authorization code from parameters of a Web answer
/// </summary>
Expand Down Expand Up @@ -136,7 +174,6 @@ public override void OpenLoginPage(string[] scopes, string redirectUri)
GenerateCSRFToken();
string responseType = AuthorizationFlow == AuthorizationFlow.AUTHORIZATION_CODE ? "code" : "token";
string uriScopes = UriUtils.WordArrayToSpaceEscapedString(scopes);
redirectUri += "code?";
string uri = authorizationEndpoint + $"?client_id={ClientData.ClientId}" + $"&response_type={responseType}" +
$"&redirect_uri={redirectUri}" + $"&scope={uriScopes}" + $"&state={state}";
Browser.OpenURL(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class OpenIDConnectService : IUpdateableService
/// </summary>
public string AccessToken { get; private set; }

private AbstractAuthorizationFlowAnswer _authorizationAnswer;

/// <summary>
/// Is true if the user of the application is currently logged in
/// </summary>
Expand Down Expand Up @@ -62,7 +64,7 @@ public class OpenIDConnectService : IUpdateableService
/// <summary>
/// Event which is raised once the login was successfully completed
/// </summary>
public event EventHandler LoginCompleted;
public event EventHandler<AbstractAuthorizationFlowAnswer> LoginCompleted;
/// <summary>
/// Event which is reaised once the logout was completed
/// </summary>
Expand Down Expand Up @@ -267,16 +269,18 @@ public async void Update()
if (OidcProvider.AuthorizationFlow == AuthorizationFlow.AUTHORIZATION_CODE)
{
string authorizationCode = OidcProvider.GetAuthorizationCode(eventArgs.RedirectParameters);
AccessToken = await OidcProvider.GetAccessTokenFromCodeAsync(authorizationCode, eventArgs.RedirectUri);
_authorizationAnswer = await OidcProvider.GetAuthorizationAnswerAsync(authorizationCode, eventArgs.RedirectUri);
AccessToken = _authorizationAnswer.access_token;
}
else
{
AccessToken = OidcProvider.GetAccessToken(eventArgs.RedirectParameters);
_authorizationAnswer = new AbstractAuthorizationFlowAnswer() { access_token = AccessToken};
}
eventArgs = null;
if (!string.IsNullOrEmpty(AccessToken))
if (_authorizationAnswer != null)
{
LoginCompleted?.Invoke(this, EventArgs.Empty);
LoginCompleted?.Invoke(this, _authorizationAnswer);
}
else
{
Expand Down