Repository navigation
Expand file tree
/
Copy pathLinkedInOAuth2Provider.cs
More file actions
60 lines (53 loc) · 2.35 KB
/
Copy pathLinkedInOAuth2Provider.cs
File metadata and controls
60 lines (53 loc) · 2.35 KB
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
using System.Collections.Generic;
using System.Xml.Linq;
using ServiceStack.Auth;
using ServiceStack.Configuration;
using ServiceStack.Text;
namespace ServiceStack.Authentication.OAuth2
{
/// <summary>
/// Create new App at: https://www.linkedin.com/secure/developer
/// </summary>
public class LinkedInOAuth2Provider : OAuth2Provider
{
public const string Name = "LinkedIn";
public const string Realm = "https://www.linkedin.com/uas/oauth2/authorization";
public LinkedInOAuth2Provider(IAppSettings appSettings)
: base(appSettings, Realm, Name)
{
this.AuthorizeUrl = this.AuthorizeUrl ?? Realm;
this.AccessTokenUrl = this.AccessTokenUrl ?? "https://www.linkedin.com/uas/oauth2/accessToken";
//Fields available at: http://developer.linkedin.com/documents/profile-fields
this.UserProfileUrl = this.UserProfileUrl
?? "https://api.linkedin.com/v1/people/~:(id,email-address,formatted-name,first-name,last-name,date-of-birth,public-profile-url,picture-url)";
if (this.Scopes.Length == 0)
{
this.Scopes = new[] {
"r_emailaddress",
"r_basicprofile"
};
}
}
protected override Dictionary<string, string> CreateAuthInfo(string accessToken)
{
var url = this.UserProfileUrl.AddQueryParam("oauth2_access_token", accessToken);
var contents = url.GetXmlFromUrl();
var xml = XDocument.Parse(contents);
var el = xml.Root;
var authInfo = new Dictionary<string, string>
{
{ "user_id", el.GetString("id") },
{ "username", el.GetString("email-address") },
{ "email", el.GetString("email-address") },
{ "name", el.GetString("formatted-name") },
{ "first_name", el.GetString("first-name") },
{ "last_name", el.GetString("last-name") },
{ "birthday", el.GetString("date-of-birth") },
{ "link", el.GetString("public-profile-url") },
{ "picture", el.GetString("picture-url") },
{ AuthMetadataProvider.ProfileUrlKey, el.GetString("picture-url") },
};
return authInfo;
}
}
}