aboutsummaryrefslogtreecommitdiff
path: root/plugins/VNLib.Plugins.Essentials.SocialOauth/src/Endpoints/GitHubOauth.cs
blob: 1fd691b333cf2dd19902d6d9dbb987a29d843b4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Essentials.SocialOauth
* File: GitHubOauth.cs 
*
* GitHubOauth.cs is part of VNLib.Plugins.Essentials.SocialOauth which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Essentials.SocialOauth is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Essentials.SocialOauth is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/

using System;
using System.Threading;
using System.Text.Json;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text.Json.Serialization;

using RestSharp;

using VNLib.Hashing;
using VNLib.Utils.Logging;
using VNLib.Plugins.Essentials.Accounts;
using VNLib.Plugins.Extensions.Loading;
using VNLib.Net.Rest.Client.Construction;


namespace VNLib.Plugins.Essentials.SocialOauth.Endpoints
{
    [ConfigurationName("github")]
    internal sealed partial class GitHubOauth : SocialOauthBase
    {
        private const string GITHUB_V3_ACCEPT = "application/vnd.github.v3+json";

        private readonly string UserEmailUrl;
        

        public GitHubOauth(PluginBase plugin, IConfigScope config) : base(plugin, config)
        {            
            UserEmailUrl = config["user_email_url"].GetString() ?? throw new KeyNotFoundException("Missing required key 'user_email_url' for github configuration");

            //Define profile endpoint, gets users required profile information
            SiteAdapter.DefineSingleEndpoint()
                .WithEndpoint<GetProfileRequest>()
                .WithMethod(Method.Get)
                .WithUrl(Config.UserDataUrl)
                .WithHeader("Accept", GITHUB_V3_ACCEPT)
                .WithHeader("Authorization", at => $"{at.AccessToken.Type} {at.AccessToken.Token}");

            //Define email endpoint, gets users email address
            SiteAdapter.DefineSingleEndpoint()
                .WithEndpoint<GetEmailRequest>()
                .WithMethod(Method.Get)
                .WithUrl(UserEmailUrl)
                .WithHeader("Authorization", at => $"{at.AccessToken.Type} {at.AccessToken.Token}")
                .WithHeader("Accept", GITHUB_V3_ACCEPT);
        }
       
        /*
         * Creates a repeatable, and source specific user id for 
         * GitHub users. This format is identical to the algorithim used
         * in the Auth0 Github connection, so it is compatible with Auth0
         */
        private static string GetUserIdFromPlatform(int userId)
        {
            return ManagedHash.ComputeHash($"github|{userId}", HashAlg.SHA1, HashEncodingMode.Hexadecimal);
        }


        protected override async Task<UserLoginData?> GetLoginDataAsync(IOAuthAccessState accessToken, CancellationToken cancellationToken)
        {
            GetProfileRequest req = new(accessToken);

            //Exec the get for the profile
            RestResponse profResponse = await SiteAdapter.ExecuteAsync(req, cancellationToken);

            if (!profResponse.IsSuccessful || profResponse.RawBytes == null)
            {
                Log.Debug("Github login data attempt responded with status code {code}", profResponse.StatusCode);
                return null;
            }

            GithubProfile profile = JsonSerializer.Deserialize<GithubProfile>(profResponse.RawBytes)!;

            if (profile.ID < 100)
            {
                Log.Debug("Github login data attempt responded with empty or invalid response body", profResponse.StatusCode);
                return null;
            }

            //Return login data
            return new()
            {
                //User-id is just the SHA 1 
                UserId = GetUserIdFromPlatform(profile.ID)
            };
        }

        protected override async Task<AccountData?> GetAccountDataAsync(IOAuthAccessState accessToken, CancellationToken cancellationToken = default)
        {
            AccountData? accountData = null;

            //Get the user's email address's
            GetEmailRequest request = new(accessToken);

            //get user's emails
            RestResponse getEmailResponse = await SiteAdapter.ExecuteAsync(request, cancellationToken);

            //Check status
            if (getEmailResponse.IsSuccessful && getEmailResponse.RawBytes != null)
            {
                //Filter emails addresses 
                foreach (EmailContainer email in JsonSerializer.Deserialize<EmailContainer[]>(getEmailResponse.RawBytes)!)
                {
                    //Capture the first primary email address and make sure its verified
                    if (email.Primary && email.Verified)
                    {
                        accountData = new()
                        {
                            //store email on current profile
                            EmailAddress = email.Email
                        };
                        goto Continue;
                    }
                }
                //No primary email found
                return null;
            }
            else
            {
                Log.Debug("Github account data request failed but GH responded with status code {code}", getEmailResponse.StatusCode);
                return null;
            }
        Continue:

            //We need to get the user's profile again
            GetProfileRequest prof = new(accessToken);

            //Exec request against site adapter
            RestResponse profResponse = await SiteAdapter.ExecuteAsync(prof, cancellationToken);
            
            if (!profResponse.IsSuccessful || profResponse.RawBytes == null)
            {
                Log.Debug("Github account data request failed but GH responded with status code {code}", profResponse.StatusCode);
                return null;
            }

            //Deserialize the profile
            GithubProfile profile = JsonSerializer.Deserialize<GithubProfile>(profResponse.RawBytes)!;

            //Get the user's name from gh profile
            string[] names = profile.FullName!.Split(" ", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

            //setup the user's profile data
            accountData.First = names.Length > 0 ? names[0] : string.Empty;
            accountData.Last = names.Length > 1 ? names[1] : string.Empty;
            return accountData;
        }

        //Requests to get required data from github

        private sealed record class GetProfileRequest(IOAuthAccessState AccessToken)
        { }

        private sealed record class GetEmailRequest(IOAuthAccessState AccessToken)
        { }

        /*
        * Matches the json result from the 
        */
        private sealed class GithubProfile
        {
            [JsonPropertyName("login")]
            public string? Username { get; set; }
            [JsonPropertyName("id")]
            public int ID { get; set; }
            [JsonPropertyName("node_id")]
            public string? NodeID { get; set; }
            [JsonPropertyName("avatar_url")]
            public string? AvatarUrl { get; set; }
            [JsonPropertyName("url")]
            public string? ProfileUrl { get; set; }
            [JsonPropertyName("type")]
            public string? Type { get; set; }
            [JsonPropertyName("name")]
            public string? FullName { get; set; }
            [JsonPropertyName("company")]
            public string? Company { get; set; }
        }
        /*
         * Matches the required data from the github email endpoint
         */
        private sealed class EmailContainer
        {
            [JsonPropertyName("email")]
            public string? Email { get; set; }
            [JsonPropertyName("primary")]
            public bool Primary { get; set; }
            [JsonPropertyName("verified")]
            public bool Verified { get; set; }
        }

    }
}