Skip to content

Commit

Permalink
Add Nintendo Switch auth methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nickelc committed Aug 26, 2020
1 parent 7da072f commit cbfac6f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Modio/Clients/AuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ public async Task<AccessToken> External(XboxAuth options)
return await RequestToken(route, options.ToContent());
}

/// <summary>
/// Request an access token on behalf of a Nintendo Switch user.
/// </summary>
public async Task<AccessToken> External(SwitchAuth options)
{
var route = Routes.ExternalSwitch();
return await RequestToken(route, options.ToContent());
}

/// <summary>
/// Request an access token on behalf of a Discord user.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions Modio/Http/Routes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public static (HttpMethod, Uri) ExternalXbox()
return (HttpMethod.Post, new Uri("external/xboxauth", UriKind.Relative));
}

public static (HttpMethod, Uri) ExternalSwitch()
{
return (HttpMethod.Post, new Uri("external/switchauth", UriKind.Relative));
}

public static (HttpMethod, Uri) ExternalDiscord()
{
return (HttpMethod.Post, new Uri("external/discordauth", UriKind.Relative));
Expand Down
51 changes: 51 additions & 0 deletions Modio/Models/Request/Auth/SwitchAuth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Net.Http;

namespace Modio
{
/// <summary>
/// See <see cref="AuthClient.External(SwitchAuth)"/>.
/// </summary>
///
/// <seealso>https://docs.mod.io/#authenticate-via-switch</seealso>
public class SwitchAuth
{
/// <summary>
/// The NSA ID supplied by the Nintendo Switch SDK.
/// </summary>
public string IdToken { get; private set; }

/// <summary>
/// The users email address.
/// </summary>
public string? Email { get; set; }

/// <summary>
/// Unix timestamp of date in which the returned token will expire.
/// </summary>
public long? ExpiredAt { get; set; }

/// <summary>
/// Initializes a new instance of <see cref="SwitchAuth"/> with the required NSA ID.
/// </summary>
public SwitchAuth(string idToken)
{
IdToken = idToken;
}

internal HttpContent ToContent()
{
var parameters = new Parameters {
{"id_token", IdToken},
};
if (Email is string email)
{
parameters.Add("email", email);
}
if (ExpiredAt is long expiredAt)
{
parameters.Add("date_expires", expiredAt.ToString());
}
return parameters.ToContent();
}
}
}

0 comments on commit cbfac6f

Please sign in to comment.