protected void InitializeTweet(CredentialsStorage credentialsStorage, ITokenizer tokenizer) { Post["/twitter/authentification/authorizationUri"] = parameters => { return TwitterOauth.GetAuthorizationUri(); }; Get["/twitter/authentification/authorizationUri"] = parameters => { return TwitterOauth.GetAuthorizationUri(); }; Get["/twitter/authentification/pin"] = parameters => { return new JavaScriptSerializer().Serialize("pin"); }; Post["/authTwitterAccaunt"] = parameters => { this.RequiresClaims(new[] { Request.Headers["Email"].First() }); string token, tokenSecret, userName; long id; TwitterOauth.GetTokens(Request.Query["oauth_token"], Request.Query["oauth_verifier"], out token, out tokenSecret, out userName, out id); var accountRepository = _storage; try { var acc = accountRepository.GetAccountById(id); var claimsUint = credentialsStorage.GetClaims(Request.Headers["Email"].First()) ?? new List<long>(); claimsUint.Add(id); string authToken; if (acc == null) { credentialsStorage.AddAccount(Request.Headers["Email"].First(), id); accountRepository.AddAccount( new Account(new TwitterCredentials(new TwitterToken(token, tokenSecret), userName, id))); authToken = tokenizer.Tokenize( new UserIdentity(Request.Headers["Email"].First(), claimsUint.Select(x => x.ToString())), Context); return new JavaScriptSerializer().Serialize(new SetTokenResponse(id, authToken)); } accountRepository.ResetTokens(userName, new TwitterToken(token, tokenSecret)); var t = claimsUint.Select(x => x.ToString()); if (t == null) { t= new List<string>(); } authToken = tokenizer.Tokenize( new UserIdentity(Request.Headers["Email"].First(), claimsUint.Select(x => x.ToString())), Context); return new JavaScriptSerializer().Serialize(new SetTokenResponse(id, authToken)); } catch (Exception) { accountRepository.AddAccount( new Account(new TwitterCredentials(new TwitterToken(token, tokenSecret), userName, id))); return Response.AsRedirect("https://mail.ru"); } }; Get["/auth"] = parameters => { string token, tokenSecret, userName; long id; TwitterOauth.GetTokens(Request.Query["oauth_token"], Request.Query["oauth_verifier"], out token, out tokenSecret, out userName, out id); var accountRepository = _storage; try { var acc = accountRepository.GetAccountById(id); if (acc == null) { accountRepository.AddAccount( new Account(new TwitterCredentials(new TwitterToken(token, tokenSecret), userName, id))); return new RedirectResponse("https://mail.ru", RedirectResponse.RedirectType.Temporary); } accountRepository.ResetTokens(userName, new TwitterToken(token, tokenSecret)); return new RedirectResponse("https://mail.ru", RedirectResponse.RedirectType.Temporary); } catch (Exception) { accountRepository.AddAccount(new Account(new TwitterCredentials(new TwitterToken(token, tokenSecret), userName, id))); return Response.AsRedirect("https://mail.ru"); } }; }
public static IUserIdentity ValidateUser(CredentialsStorage storage, string userName, string password) { return !storage.Validate(userName, password) ? null : new UserIdentity(userName, storage.GetClaims(userName).Select(item => item.ToString())); }