-
Notifications
You must be signed in to change notification settings - Fork 5
/
MyTwitterAuthProvider.cs
211 lines (173 loc) · 9.92 KB
/
MyTwitterAuthProvider.cs
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
using System.Collections.Generic;
using ExpressBase.Data;
using ExpressBase.Objects.ServiceStack_Artifacts;
using ServiceStack;
using ServiceStack.Auth;
using ServiceStack.Configuration;
using ServiceStack.Web;
using ExpressBase.Common;
using ExpressBase.Common.Data;
using ExpressBase.Common.Constants;
using ExpressBase.Common.Structures;
using ExpressBase.Common.ServiceStack.Auth;
using Newtonsoft.Json;
using System;
using System.Data.Common;
using System.Threading.Tasks;
using ExpressBase.Common.Extensions;
namespace ExpressBase.ServiceStack
{
public class MyTwitterAuthProvider : TwitterAuthProvider
{
public MyTwitterAuthProvider(IAppSettings settings) : base(settings) { }
public override object Authenticate(IServiceBase authService, IAuthSession session, Authenticate request)
{
var objret = base.Authenticate(authService, session, request);
// no need if email present change to email
if (!string.IsNullOrEmpty(session.ProviderOAuthAccess[0].DisplayName))
{
EbConnectionFactory InfraConnectionFactory = authService.ResolveService<IEbConnectionFactory>() as EbConnectionFactory;
if (!string.IsNullOrEmpty(session.ProviderOAuthAccess[0].Email))
{
string b = string.Empty;
try
{
bool unique = false;
string sql1 = "SELECT id, pwd FROM eb_tenants WHERE email ~* @email";
DbParameter[] parameters2 = { InfraConnectionFactory.DataDB.GetNewParameter("email", EbDbTypes.String, session.ProviderOAuthAccess[0].Email) };
EbDataTable dt = InfraConnectionFactory.DataDB.DoQuery(sql1, parameters2);
if (dt.Rows.Count > 0)
{
unique = false;
}
else
unique = true;
//string sql11 = "SELECT id, pwd FROM eb_tenants WHERE email ~* @email";
//DbParameter[] parameters22 = { InfraConnectionFactory.DataDB.GetNewParameter("email", EbDbTypes.String, session.ProviderOAuthAccess[0].Email) };
//int dtq = InfraConnectionFactory.DataDB.DoNonQuery(sql11, parameters22);
if (unique == true)
{
DbParameter[] parameter1 = {
InfraConnectionFactory.DataDB.GetNewParameter("email", EbDbTypes.String, session.ProviderOAuthAccess[0].Email),
InfraConnectionFactory.DataDB.GetNewParameter("name", EbDbTypes.String, session.ProviderOAuthAccess[0].UserName),
InfraConnectionFactory.DataDB.GetNewParameter("twitterid", EbDbTypes.String, (session.ProviderOAuthAccess[0].UserId).ToString()),
InfraConnectionFactory.DataDB.GetNewParameter("password", EbDbTypes.String, (session.ProviderOAuthAccess[0].UserId.ToString()+session.ProviderOAuthAccess[0].Email.ToString()).ToMD5Hash()),
};
EbDataTable dtbl = InfraConnectionFactory.DataDB.DoQuery(@"INSERT INTO eb_tenants (email,fullname,twitter_id,pwd)
VALUES
(:email,:name,:twitterid,:password) RETURNING id;", parameter1);
}
SocialSignup sco_signup = new SocialSignup
{
AuthProvider = session.ProviderOAuthAccess[0].Provider,
Country = session.ProviderOAuthAccess[0].Country,
Email = session.ProviderOAuthAccess[0].Email,
Social_id = (session.ProviderOAuthAccess[0].UserId).ToString(),
Fullname = session.ProviderOAuthAccess[0].UserName,
//IsVerified = session.IsAuthenticated,
Pauto = (session.ProviderOAuthAccess[0].UserId.ToString() + session.ProviderOAuthAccess[0].Email.ToString()).ToMD5Hash(),
UniqueEmail = unique,
};
b = JsonConvert.SerializeObject(sco_signup);
return authService.Redirect(SuccessRedirectUrlFilter(this, string.Format("http://localhost:41500/social_oauth?scosignup={0}", b)));
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message + e.StackTrace);
}
}
//using (var con = _InfraDb.DataDB.GetNewConnection())
//{
// con.Open();
// var cmd = _InfraDb.DataDB.GetNewCommand(con, "INSERT INTO eb_users (email,firstname,socialid,prolink) VALUES(@email, @firstname,@socialid,@prolink) ON CONFLICT(socialid) DO UPDATE SET loginattempts = eb_users.loginattempts + EXCLUDED.loginattempts RETURNING eb_users.loginattempts");
// cmd.Parameters.Add(_InfraDb.DataDB.GetNewParameter("email", EbDbTypes.String, session.ProviderOAuthAccess[0].Email));
// cmd.Parameters.Add(_InfraDb.DataDB.GetNewParameter("firstname", EbDbTypes.String, session.ProviderOAuthAccess[0].DisplayName));
// cmd.Parameters.Add(_InfraDb.DataDB.GetNewParameter("socialid", EbDbTypes.String, session.ProviderOAuthAccess[0].UserName));
// cmd.Parameters.Add(_InfraDb.DataDB.GetNewParameter("prolink", EbDbTypes.String, session.ProviderOAuthAccess[0].Items["profileUrl"]));
// cmd.ExecuteNonQuery();
//}
//(session as CustomUserSession).Company = CoreConstants.EXPRESSBASE;
//(session as CustomUserSession).WhichConsole = "tc";
//return authService.Redirect(SuccessRedirectUrlFilter(this, "http://localhost:5000/Ext/AfterSignInSocial?email=" + session.Email + "&socialId=" + session.UserName + "&provider=" + session.AuthProvider + "&providerToken=" + session.ProviderOAuthAccess[0].AccessTokenSecret));
}
return objret;
}
public override string CreateOrMergeAuthSession(IAuthSession session, IAuthTokens tokens)
{
return base.CreateOrMergeAuthSession(session, tokens);
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override bool IsAccountLocked(IAuthRepository authRepo, IUserAuth userAuth, IAuthTokens tokens = null)
{
return base.IsAccountLocked(authRepo, userAuth, tokens);
}
public override bool IsAuthorized(IAuthSession session, IAuthTokens tokens, Authenticate request = null)
{
return base.IsAuthorized(session, tokens, request);
}
public override void LoadUserOAuthProvider(IAuthSession authSession, IAuthTokens tokens)
{
base.LoadUserOAuthProvider(authSession, tokens);
}
public override object Logout(IServiceBase service, Authenticate request)
{
return base.Logout(service, request);
}
//************is this for authanticate********
public override IHttpResult OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary<string, string> authInfo)
{
return base.OnAuthenticated(authService, session, tokens, authInfo);
}
public override void OnFailedAuthentication(IAuthSession session, IRequest httpReq, IResponse httpRes)
{
base.OnFailedAuthentication(session, httpReq, httpRes);
}
public override Task OnFailedAuthenticationAsync(IAuthSession session, IRequest httpReq, IResponse httpRes)
{
return base.OnFailedAuthenticationAsync(session, httpReq, httpRes);
}
public override string ToString()
{
return base.ToString();
}
protected override object ConvertToClientError(object failedResult, bool isHtml)
{
return base.ConvertToClientError(failedResult, isHtml);
}
protected override bool EmailAlreadyExists(IAuthRepository authRepo, IUserAuth userAuth, IAuthTokens tokens = null)
{
return base.EmailAlreadyExists(authRepo, userAuth, tokens);
}
protected override string GetAuthRedirectUrl(IServiceBase authService, IAuthSession session)
{
return base.GetAuthRedirectUrl(authService, session);
}
protected override IAuthRepository GetAuthRepository(IRequest req)
{
return base.GetAuthRepository(req);
}
protected override string GetReferrerUrl(IServiceBase authService, IAuthSession session, Authenticate request = null)
{
return base.GetReferrerUrl(authService, session, request);
}
protected override void LoadUserAuthInfo(AuthUserSession userSession, IAuthTokens tokens, Dictionary<string, string> authInfo)
{
base.LoadUserAuthInfo(userSession, tokens, authInfo);
}
protected override bool UserNameAlreadyExists(IAuthRepository authRepo, IUserAuth userAuth, IAuthTokens tokens = null)
{
return base.UserNameAlreadyExists(authRepo, userAuth, tokens);
}
protected override IHttpResult ValidateAccount(IServiceBase authService, IAuthRepository authRepo, IAuthSession session, IAuthTokens tokens)
{
return base.ValidateAccount(authService, authRepo, session, tokens);
}
}
}