forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleOAuth2Provider.cs
More file actions
64 lines (58 loc) · 2.72 KB
/
GoogleOAuth2Provider.cs
File metadata and controls
64 lines (58 loc) · 2.72 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
61
62
63
64
using System.Collections.Generic;
using ServiceStack.Auth;
using ServiceStack.Configuration;
using ServiceStack.Text;
namespace ServiceStack.Authentication.OAuth2
{
/*
Create an OAuth2 App at: https://code.google.com/apis/console/
The Apps Callback URL should match the CallbackUrl here.
Google OAuth2 info: https://developers.google.com/accounts/docs/OAuth2Login
Google OAuth2 Scopes from: https://www.googleapis.com/discovery/v1/apis/oauth2/v2/rest?fields=auth(oauth2(scopes))
https://www.googleapis.com/auth/plus.login: Know your name, basic info, and list of people you're connected to on Google+
https://www.googleapis.com/auth/plus.me Know who you are on Google+
https://www.googleapis.com/auth/userinfo.email View your email address
https://www.googleapis.com/auth/userinfo.profile View basic information about your account
*/
public class GoogleOAuth2Provider : OAuth2Provider
{
public const string Name = "GoogleOAuth";
public const string Realm = "https://accounts.google.com/o/oauth2/auth";
public GoogleOAuth2Provider(IAppSettings appSettings)
: base(appSettings, Realm, Name)
{
this.AuthorizeUrl = this.AuthorizeUrl ?? Realm;
this.AccessTokenUrl = this.AccessTokenUrl ?? "https://accounts.google.com/o/oauth2/token";
this.UserProfileUrl = this.UserProfileUrl ?? "https://www.googleapis.com/oauth2/v1/userinfo";
if (this.Scopes.Length == 0)
{
this.Scopes = new[] {
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email"
};
}
}
protected override Dictionary<string, string> CreateAuthInfo(string accessToken)
{
var url = this.UserProfileUrl.AddQueryParam("access_token", accessToken);
string json = url.GetJsonFromUrl();
var obj = JsonObject.Parse(json);
var authInfo = new Dictionary<string, string>
{
{ "user_id", obj["id"] },
{ "username", obj["email"] },
{ "email", obj["email"] },
{ "name", obj["name"] },
{ "first_name", obj["given_name"] },
{ "last_name", obj["family_name"] },
{ "gender", obj["gender"] },
{ "birthday", obj["birthday"] },
{ "link", obj["link"] },
{ "picture", obj["picture"] },
{ "locale", obj["locale"] },
{ AuthMetadataProvider.ProfileUrlKey, obj["picture"] },
};
return authInfo;
}
}
}