From cbfac6f57fe2d805be6a8ddc60f7c44da000edde Mon Sep 17 00:00:00 2001 From: Constantin Nickel Date: Wed, 26 Aug 2020 12:25:35 +0200 Subject: [PATCH] Add Nintendo Switch auth methods --- Modio/Clients/AuthClient.cs | 9 +++++ Modio/Http/Routes.cs | 5 +++ Modio/Models/Request/Auth/SwitchAuth.cs | 51 +++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 Modio/Models/Request/Auth/SwitchAuth.cs diff --git a/Modio/Clients/AuthClient.cs b/Modio/Clients/AuthClient.cs index 65c822a..3e1f9db 100644 --- a/Modio/Clients/AuthClient.cs +++ b/Modio/Clients/AuthClient.cs @@ -86,6 +86,15 @@ public async Task External(XboxAuth options) return await RequestToken(route, options.ToContent()); } + /// + /// Request an access token on behalf of a Nintendo Switch user. + /// + public async Task External(SwitchAuth options) + { + var route = Routes.ExternalSwitch(); + return await RequestToken(route, options.ToContent()); + } + /// /// Request an access token on behalf of a Discord user. /// diff --git a/Modio/Http/Routes.cs b/Modio/Http/Routes.cs index c8cfb07..eaea39f 100644 --- a/Modio/Http/Routes.cs +++ b/Modio/Http/Routes.cs @@ -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)); diff --git a/Modio/Models/Request/Auth/SwitchAuth.cs b/Modio/Models/Request/Auth/SwitchAuth.cs new file mode 100644 index 0000000..86b04c9 --- /dev/null +++ b/Modio/Models/Request/Auth/SwitchAuth.cs @@ -0,0 +1,51 @@ +using System.Net.Http; + +namespace Modio +{ + /// + /// See . + /// + /// + /// https://docs.mod.io/#authenticate-via-switch + public class SwitchAuth + { + /// + /// The NSA ID supplied by the Nintendo Switch SDK. + /// + public string IdToken { get; private set; } + + /// + /// The users email address. + /// + public string? Email { get; set; } + + /// + /// Unix timestamp of date in which the returned token will expire. + /// + public long? ExpiredAt { get; set; } + + /// + /// Initializes a new instance of with the required NSA ID. + /// + 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(); + } + } +}