public RegistrationModule(ITokenizer tokenizer, CredentialsStorage storage)
            : base("/registration")
        {
            Post["/"] = x =>
            {
                var userName = Request.Headers["UserName"].First();
                var password = Request.Headers["Password"].First();

                var userIdentity = UserDatabase.CreateUser(storage, userName, password);

                if (userIdentity == null)
                {
                    return HttpStatusCode.Unauthorized;
                }

                var token = tokenizer.Tokenize(userIdentity, Context);

                return new
                {
                    Token = token,
                };
            };
        }
예제 #2
0
        public AuthModule(ITokenizer tokenizer, CredentialsStorage storage)
            : base("/auth")
        {
            Post["/"] = x =>
            {
                var userName = Request.Headers["UserName"].First();
                var password = Request.Headers["Password"].First();

                var userIdentity = UserDatabase.ValidateUser(storage, userName, password);

                if (userIdentity == null)
                {
                    return HttpStatusCode.Unauthorized;
                }

                var token = tokenizer.Tokenize(userIdentity, Context);

                return new
                {
                    Token = token,
                    UserId = userIdentity.Claims.ElementAt(0)
                };
            };

            Get["/validation"] = _ =>
            {
                this.RequiresAuthentication();
                return "Yay! You are authenticated!";
            };

            Get["/admin"] = _ =>
            {

                this.RequiresClaims(new[] { "admin" });
                return "Yay! You are authorized!";
            };
        }
        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 TwitterAuthentificationModule(CredentialsStorage credentialsStorage, ITokenizer tokenizer, IStorage storage)
 {
     _storage = storage;
     InitializeTweet(credentialsStorage, tokenizer);
 }
예제 #5
0
 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()));
 }
예제 #6
0
 public static IUserIdentity CreateUser(CredentialsStorage storage, string userName, string password)
 {
     storage.CreateUser(userName,password);
     return new UserIdentity(userName, new List<string>());
 }