예제 #1
0
        public static void RedirectToAuthorization(this WebOperationContext context, ServerWithAuthorizationCode server, Uri redirectionUri, ResourceOwner resourceOwner)
        {

            context.OutgoingResponse.StatusCode = HttpStatusCode.Redirect;
            SetRedirectUriInToken(server, resourceOwner, redirectionUri);
            context.OutgoingResponse.Location = GetAuthorizationLocation(server, redirectionUri, resourceOwner);
        }
예제 #2
0
        public void TokenComperator()
        {
            var server1 = new ServerWithAuthorizationCode("test", "testsecret",
                                                          new Uri("http://example.org/foo"),
                                                          new Uri("http://example.org/access"),
                                                          new Uri("http://example.org/test2"));
            var server2 = new ServerWithAuthorizationCode("test2", "testsecret",
                                                          new Uri("http://example.org/fo32o"),
                                                          new Uri("http://example.org/access"),
                                                          new Uri("http://example.org/tesdst2"));
            var resourceOwner1 = new ResourceOwner("testmann1");
            var resourceOwner2 = new ResourceOwner("testmann2");

            var token1 = new Token(server1,resourceOwner1);
            var token2 = new Token(server1,resourceOwner2);
            var token3 = new Token(server2,resourceOwner1);
            var token4 = new Token(server2,resourceOwner2);
            var token5 = new Token(server1,resourceOwner1);

            token1.Equals(token1).Should().BeTrue();
            token1.Equals(token2).Should().BeFalse();
            token1.Equals(token3).Should().BeFalse();
            token1.Equals(token4).Should().BeFalse();
            token1.Equals(token5).Should().BeTrue();

            (token1 == token5).Should().BeTrue();
            (token1 == token2).Should().BeFalse();
            (token1 != token2).Should().BeTrue();
            (token1 == null).Should().BeFalse();

            token1.Equals(42).Should().BeFalse();
            token1.Equals(null).Should().BeFalse();

        }
예제 #3
0
        public void GetToken()
        {
            var server1 = new ServerWithAuthorizationCode("test", "testsecret",
                                                          new Uri("http://example.org/foo"),
                                                          new Uri("http://example.org/access"),
                                                          new Uri("http://example.org/test2"));
            var resourceOwner1 = new ResourceOwner("testmann1");
            Tokens.CleanUpForTests();
            var token1 = Tokens.GetToken(server1, resourceOwner1);

            token1.Should().NotBeNull();
            token1.ResourceOwner.Should().Be(resourceOwner1);
            token1.Server.Should().Be(server1);
            token1.AuthorizationCode.Should().BeNullOrEmpty();
            token1.AccessToken.Should().BeNullOrEmpty();
            token1.RefreshToken.Should().BeNullOrEmpty();

            token1.AuthorizationCode = "AuthorizationCode";
            token1.AccessToken = "AccessToken";
            token1.RefreshToken = "RefreshToken";
            token1.Expires = DateTime.Now;

            var token2 = Tokens.GetToken(server1, resourceOwner1);

            token2.AuthorizationCode.Should().Be("AuthorizationCode");
            token2.AccessToken.Should().Be("AccessToken");
            token2.RefreshToken.Should().Be("RefreshToken");
            DateTime.Now.Subtract(token2.Expires).Should().BeLessOrEqualTo(new TimeSpan(0, 0, 1, 0));

        }
예제 #4
0
        public void GetToken()
        {
            var server1 = new ServerWithAuthorizationCode("test", "testsecret",
                                                          new Uri("http://example.org/foo"),
                                                          new Uri("http://example.org/access"),
                                                          new Uri("http://example.org/test2"));
            var resourceOwner1 = new ResourceOwner("testmann1");

            Tokens.CleanUpForTests();
            var token1 = Tokens.GetToken(server1, resourceOwner1);

            token1.Should().NotBeNull();
            token1.ResourceOwner.Should().Be(resourceOwner1);
            token1.Server.Should().Be(server1);
            token1.AuthorizationCode.Should().BeNullOrEmpty();
            token1.AccessToken.Should().BeNullOrEmpty();
            token1.RefreshToken.Should().BeNullOrEmpty();

            token1.AuthorizationCode = "AuthorizationCode";
            token1.AccessToken       = "AccessToken";
            token1.RefreshToken      = "RefreshToken";
            token1.Expires           = DateTime.Now;

            var token2 = Tokens.GetToken(server1, resourceOwner1);

            token2.AuthorizationCode.Should().Be("AuthorizationCode");
            token2.AccessToken.Should().Be("AccessToken");
            token2.RefreshToken.Should().Be("RefreshToken");
            DateTime.Now.Subtract(token2.Expires).Should().BeLessOrEqualTo(new TimeSpan(0, 0, 1, 0));
        }
예제 #5
0
        public void TokenToXElement()
        {
            var server1 = new ServerWithAuthorizationCode("test", "testsecret",
                                                          new Uri("http://example.org/foo"),
                                                          new Uri("http://example.org/access"),
                                                          new Uri("http://example.org/test2"));
            var resourceOwner1 = new ResourceOwner("testmann1");

            var token = new Token(server1, resourceOwner1);

            token.AuthorizationCode = "auth1";
            token.AccessToken       = "access1";
            token.RefreshToken      = "refresht";
            token.RedirectUri       = new Uri("http://example.org/redirect");

            var element1 = token.ToXElement();

            element1.Name.ToString().Should().Be("Token");
            element1.Element("Server").Value.Should().Be(server1.Guid.ToString());
            element1.Element("ResourceOwner").Value.Should().Be(resourceOwner1.Name);
            element1.Element("AuthorizationCode").Value.Should().Be("auth1");
            element1.Element("AccessToken").Value.Should().Be("access1");
            element1.Element("RefreshToken").Value.Should().Be("refresht");
            element1.Element("RedirectUri").Value.Should().Be("http://example.org/redirect");
        }
예제 #6
0
        public void TokenComperator()
        {
            var server1 = new ServerWithAuthorizationCode("test", "testsecret",
                                                          new Uri("http://example.org/foo"),
                                                          new Uri("http://example.org/access"),
                                                          new Uri("http://example.org/test2"));
            var server2 = new ServerWithAuthorizationCode("test2", "testsecret",
                                                          new Uri("http://example.org/fo32o"),
                                                          new Uri("http://example.org/access"),
                                                          new Uri("http://example.org/tesdst2"));
            var resourceOwner1 = new ResourceOwner("testmann1");
            var resourceOwner2 = new ResourceOwner("testmann2");

            var token1 = new Token(server1, resourceOwner1);
            var token2 = new Token(server1, resourceOwner2);
            var token3 = new Token(server2, resourceOwner1);
            var token4 = new Token(server2, resourceOwner2);
            var token5 = new Token(server1, resourceOwner1);

            token1.Equals(token1).Should().BeTrue();
            token1.Equals(token2).Should().BeFalse();
            token1.Equals(token3).Should().BeFalse();
            token1.Equals(token4).Should().BeFalse();
            token1.Equals(token5).Should().BeTrue();

            (token1 == token5).Should().BeTrue();
            (token1 == token2).Should().BeFalse();
            (token1 != token2).Should().BeTrue();
            (token1 == null).Should().BeFalse();

            token1.Equals(42).Should().BeFalse();
            token1.Equals(null).Should().BeFalse();
        }
예제 #7
0
        private static string GetAuthorizationLocation(ServerWithAuthorizationCode server, Uri redirectionUri, ResourceOwner resourceOwner)
        {
            var scopes = server.Scopes.Aggregate("", (current, scope) => current + (scope + " ")).Trim();

            return server.AuthorizationRequestUri + "?response_type=code&client_id=" +
                   server.ClientId +
                   "&state=" + server.Guid + "_" + resourceOwner.Guid +
                   "&scope=" + HttpUtility.UrlEncode(scopes) +
                   "&redirect_uri=" + HttpUtility.UrlEncode(redirectionUri.ToString());
        }
예제 #8
0
        private static string GetAuthorizationLocation(ServerWithAuthorizationCode server, Uri redirectionUri, ResourceOwner resourceOwner)
        {
            var scopes = server.Scopes.Aggregate("", (current, scope) => current + (scope + " ")).Trim();

            return(server.AuthorizationRequestUri + "?response_type=code&client_id=" +
                   server.ClientId +
                   "&state=" + server.Guid + "_" + resourceOwner.Guid +
                   "&scope=" + HttpUtility.UrlEncode(scopes) +
                   "&redirect_uri=" + HttpUtility.UrlEncode(redirectionUri.ToString()));
        }
        private void CmdServerCreateClick(object sender, EventArgs e)
        {
            var scopes    = txtScope.Text.Split(',');
            var scopeList = scopes.Select(scope => scope.Trim()).ToList();

            _server = ServersWithAuthorizationCode.Add(txtServerClientId.Text,
                                                       txtClientSharedSecret.Text,
                                                       new Uri(txtServerAuthorizationUri.Text),
                                                       new Uri(txtServerAccessUri.Text),
                                                       new Uri(txtServerRedirectionUri.Text),
                                                       scopeList);
            _server.Version    = (Server.OAuthVersion)Enum.Parse(typeof(Server.OAuthVersion), cbVersion.Text);
            lblServerGUID.Text = _server.Guid.ToString();
        }
예제 #10
0
        public void ServerFromXElementInvalid()
        {
            var element = new XElement("Server");

            element.Add(new XAttribute("type", "incorrectType"));
            element.Add(new XElement("Guid", "f1287c12-1cf3-45b3-ac29-5bfce34b2145"));
            element.Add(new XElement("ClientId", "myspecialClientId"));
            element.Add(new XElement("ClientSharedSecret", "acsecret123"));
            element.Add(new XElement("Scopes", new XElement("Scope", "foobar")));
            element.Add(new XElement("AuthorizationUri", "http://example.com/anotherfunnyUri"));
            element.Add(new XElement("AccessTokenUri", "http://example.com/anotherfunnyUri2"));
            element.Add(new XElement("RedirectionUri", "http://example.com/behappy"));

            var server = ServerWithAuthorizationCode.FromXElement(element);
        }
예제 #11
0
        public void SetUp()
        {
            Tokens.CleanUpForTests();
            ResourceOwners.CleanUpForTests();
            ServersWithAuthorizationCode.CleanUpForTests();

            var authorizationRequestUri = new Uri("http://example.org/GetAccessAndRefreshTokenTest/Authorization");
            var accessTokenUri = new Uri("http://example.org/GetAccessAndRefreshTokenTest/Access");
            var redirectionUri = new Uri("http://example.org/GetAccessAndRefreshTokenTest/redirectionUri");
            _server = ServersWithAuthorizationCode.Add("123456789", "testsecret", authorizationRequestUri,accessTokenUri, redirectionUri);
            _resourceOwner = ResourceOwners.Add("Test");
            _token = Tokens.GetToken(_server, _resourceOwner);
            _token.RedirectUri = _server.RedirectionUri;
            _token.AuthorizationCode = "Aplx10BeZQQYbYS6WxSbIA";
        }
        public void SetUp()
        {
            Tokens.CleanUpForTests();
            ResourceOwners.CleanUpForTests();
            ServersWithAuthorizationCode.CleanUpForTests();

            var authorizationRequestUri = new Uri("http://example.org/GetAccessAndRefreshTokenTest/Authorization");
            var accessTokenUri          = new Uri("http://example.org/GetAccessAndRefreshTokenTest/Access");
            var redirectionUri          = new Uri("http://example.org/GetAccessAndRefreshTokenTest/redirectionUri");

            _server                  = ServersWithAuthorizationCode.Add("123456789", "testsecret", authorizationRequestUri, accessTokenUri, redirectionUri);
            _resourceOwner           = ResourceOwners.Add("Test");
            _token                   = Tokens.GetToken(_server, _resourceOwner);
            _token.RedirectUri       = _server.RedirectionUri;
            _token.AuthorizationCode = "Aplx10BeZQQYbYS6WxSbIA";
        }
예제 #13
0
        public void ServerToXElement()
        {
            var authorizationuri = new Uri("http://example.com/auth");
            var accessUri        = new Uri("http://example.com/auth/access");
            var redirectionUri   = new Uri("http://example.com/redirect");
            var server           = new ServerWithAuthorizationCode("clientid123", "myfunnysecret", authorizationuri, accessUri, redirectionUri, new List <String>()
            {
                "funnyscope"
            });
            var element = server.ToXElement();

            element.Should().NotBeNull();
            element.Name.ToString().Should().Be("Server");

            element.Attribute("type").Should().NotBeNull();
            element.Attribute("type").Value.Should().Be("AuthorizationCode");

            element.Element("Guid").Should().NotBeNull();
            element.Element("Guid").Value.Should().Be(server.Guid.ToString());

            element.Element("ClientId").Should().NotBeNull();
            element.Element("ClientId").Value.Should().Be("clientid123");

            element.Element("ClientSharedSecret").Should().NotBeNull();
            element.Element("ClientSharedSecret").Value.Should().Be("myfunnysecret");

            var scopes = element.Element("Scopes");

            scopes.Should().NotBeNull();
            scopes.Element("Scope").Value.Should().NotBeNull();
            scopes.Element("Scope").Value.Should().Be("funnyscope");

            element.Element("AuthorizationUri").Should().NotBeNull();
            element.Element("AuthorizationUri").Value.Should().Be(authorizationuri.ToString());

            element.Element("AccessTokenUri").Should().NotBeNull();
            element.Element("AccessTokenUri").Value.Should().Be(accessUri.ToString());

            element.Element("RedirectionUri").Should().NotBeNull();
            element.Element("RedirectionUri").Value.Should().Be(redirectionUri.ToString());
        }
예제 #14
0
        public void ServerFromXElement()
        {
            var element = new XElement("Server");

            element.Add(new XAttribute("type", "AuthorizationCode"));
            element.Add(new XElement("Guid", "f1287c12-1cf3-45b3-ac29-5bfce34b2145"));
            element.Add(new XElement("ClientId", "myspecialClientId"));
            element.Add(new XElement("ClientSharedSecret", "acsecret123"));
            element.Add(new XElement("Scopes", new XElement("Scope", "foobar")));
            element.Add(new XElement("AuthorizationUri", "http://example.com/anotherfunnyUri"));
            element.Add(new XElement("AccessTokenUri", "http://example.com/anotherfunnyUri2"));
            element.Add(new XElement("RedirectionUri", "http://example.com/behappy"));

            var server = ServerWithAuthorizationCode.FromXElement(element);

            server.ClientId.Should().Be("myspecialClientId");
            server.ClientSharedSecret.Should().Be("acsecret123");
            server.Guid.ToString().Should().Be("f1287c12-1cf3-45b3-ac29-5bfce34b2145");
            server.AuthorizationRequestUri.ToString().Should().Be("http://example.com/anotherfunnyUri");
            server.AccessTokenRequestUri.ToString().Should().Be("http://example.com/anotherfunnyUri2");
            server.RedirectionUri.ToString().Should().Be("http://example.com/behappy");
            server.Scopes.FirstOrDefault(item => item == "foobar").Should().NotBeNull();
        }
예제 #15
0
        public void ServerToXElement()
        {
            var authorizationuri = new Uri("http://example.com/auth");
            var accessUri = new Uri("http://example.com/auth/access");
            var redirectionUri = new Uri("http://example.com/redirect");
            var server = new ServerWithAuthorizationCode("clientid123", "myfunnysecret", authorizationuri, accessUri, redirectionUri, new List<String>(){"funnyscope"});
            var element = server.ToXElement();

            element.Should().NotBeNull();
            element.Name.ToString().Should().Be("Server");

            element.Attribute("type").Should().NotBeNull();
            element.Attribute("type").Value.Should().Be("AuthorizationCode");

            element.Element("Guid").Should().NotBeNull();
            element.Element("Guid").Value.Should().Be(server.Guid.ToString());

            element.Element("ClientId").Should().NotBeNull();
            element.Element("ClientId").Value.Should().Be("clientid123");

            element.Element("ClientSharedSecret").Should().NotBeNull();
            element.Element("ClientSharedSecret").Value.Should().Be("myfunnysecret");

            var scopes = element.Element("Scopes");
            scopes.Should().NotBeNull();
            scopes.Element("Scope").Value.Should().NotBeNull();
            scopes.Element("Scope").Value.Should().Be("funnyscope");

            element.Element("AuthorizationUri").Should().NotBeNull();
            element.Element("AuthorizationUri").Value.Should().Be(authorizationuri.ToString());

            element.Element("AccessTokenUri").Should().NotBeNull();
            element.Element("AccessTokenUri").Value.Should().Be(accessUri.ToString());

            element.Element("RedirectionUri").Should().NotBeNull();
            element.Element("RedirectionUri").Value.Should().Be(redirectionUri.ToString());
        }
예제 #16
0
 public static void RedirectToAuthorization(this WebOperationContext context, ServerWithAuthorizationCode server, Uri redirectionUri, ResourceOwner resourceOwner)
 {
     context.OutgoingResponse.StatusCode = HttpStatusCode.Redirect;
     SetRedirectUriInToken(server, resourceOwner, redirectionUri);
     context.OutgoingResponse.Location = GetAuthorizationLocation(server, redirectionUri, resourceOwner);
 }
예제 #17
0
 private static void SetRedirectUriInToken(ServerWithAuthorizationCode server, ResourceOwner resourceOwner, Uri redirectionUri)
 {
     var token = Tokens.GetToken(server, resourceOwner);
     token.RedirectUri = redirectionUri;
 }
 public NoAuthorizationCodeException(ServerWithAuthorizationCode server, ResourceOwner resourceOwner)
 {
     Server        = server;
     ResourceOwner = resourceOwner;
 }
예제 #19
0
        public void TokenToXElement()
        {
            var server1 = new ServerWithAuthorizationCode("test", "testsecret",
                                                          new Uri("http://example.org/foo"),
                                                          new Uri("http://example.org/access"),
                                                          new Uri("http://example.org/test2"));
            var resourceOwner1 = new ResourceOwner("testmann1");

            var token = new Token(server1, resourceOwner1);
            token.AuthorizationCode = "auth1";
            token.AccessToken = "access1";
            token.RefreshToken = "refresht";
            token.RedirectUri = new Uri("http://example.org/redirect");
            
            var element1 = token.ToXElement();

            element1.Name.ToString().Should().Be("Token");
            element1.Element("Server").Value.Should().Be(server1.Guid.ToString());
            element1.Element("ResourceOwner").Value.Should().Be(resourceOwner1.Name);
            element1.Element("AuthorizationCode").Value.Should().Be("auth1");
            element1.Element("AccessToken").Value.Should().Be("access1");
            element1.Element("RefreshToken").Value.Should().Be("refresht");
            element1.Element("RedirectUri").Value.Should().Be("http://example.org/redirect");
            

        }
예제 #20
0
 public static void RedirectToAuthorization(this IWebOperationContext context, ServerWithAuthorizationCode server, ResourceOwner resourceOwner)
 {
     context.RedirectToAuthorization(server, server.RedirectionUri, resourceOwner);
 }
예제 #21
0
        private static void SetRedirectUriInToken(ServerWithAuthorizationCode server, ResourceOwner resourceOwner, Uri redirectionUri)
        {
            var token = Tokens.GetToken(server, resourceOwner);

            token.RedirectUri = redirectionUri;
        }
예제 #22
0
 public static void RedirectToAuthorization(this IWebOperationContext context, ServerWithAuthorizationCode server, ResourceOwner resourceOwner)
 {
     context.RedirectToAuthorization(server, server.RedirectionUri, resourceOwner);
 }