Example #1
0
        public bool TryAuthenticate(string code, string state, string verify, out GithubAccessToken token)
        {
            token = null;

            if (!String.Equals(state, verify, StringComparison.Ordinal))
            {
                return false;
            }

            var post = String.Concat("client_id=", Configuration.ClientId, "&client_secret=", Configuration.ClientSecret, "&code=", code);

            try
            {
                var tokenRequest = new JsonWebRequest("https://github.com/login/oauth/access_token") { PostData = post };
                var response = tokenRequest.Request<TokenResponse>();

                token = new GithubAccessToken() { ReturnUrl = state.Length > 32 ? state.Substring(32) : null, Scope = response.scope, Type = response.token_type, Value = response.access_token };
            }
            catch (Exception e)
            {
                var t = e;
            }

            return token != null;
        }
Example #2
0
        public bool TryAuthenticate(string code, string state, string verify, out GithubAccessToken token)
        {
            token = null;

            if (!String.Equals(state, verify, StringComparison.Ordinal))
            {
                return(false);
            }

            var post = String.Concat("client_id=", Configuration.ClientId, "&client_secret=", Configuration.ClientSecret, "&code=", code);

            try
            {
                var tokenRequest = new JsonWebRequest("https://github.com/login/oauth/access_token")
                {
                    PostData = post
                };
                var response = tokenRequest.Request <TokenResponse>();

                token = new GithubAccessToken()
                {
                    ReturnUrl = state.Length > 32 ? state.Substring(32) : null, Scope = response.scope, Type = response.token_type, Value = response.access_token
                };
            }
            catch (Exception e)
            {
                var t = e;
            }

            return(token != null);
        }
Example #3
0
        public GithubUser GetUser(string accessToken)
        {
            var authorization = "Bearer " + accessToken;

            var userRequest = new JsonWebRequest("https://api.github.com/user") { Authorization = authorization };
            var user = userRequest.Request<User>();

            var emailRequest = new JsonWebRequest("https://api.github.com/user/emails") { Accept = "application/vnd.github.v3", Authorization = authorization };
            var emails = emailRequest.Request<UserEmail[]>();

            var primaryEmail = emails.Where(e => e.Primary).Select(e => e.Email).FirstOrDefault();

            return new GithubUser() { Company = user.Company, Login = user.Login, Name = user.Name, PrimaryEmail = primaryEmail };
        }
Example #4
0
        public GithubUser GetUser(string accessToken)
        {
            var authorization = "Bearer " + accessToken;

            var userRequest = new JsonWebRequest("https://api.github.com/user")
            {
                Authorization = authorization
            };
            var user = userRequest.Request <User>();

            var emailRequest = new JsonWebRequest("https://api.github.com/user/emails")
            {
                Accept = "application/vnd.github.v3", Authorization = authorization
            };
            var emails = emailRequest.Request <UserEmail[]>();

            var primaryEmail = emails.Where(e => e.Primary).Select(e => e.Email).FirstOrDefault();

            return(new GithubUser()
            {
                Company = user.Company, Login = user.Login, Name = user.Name, PrimaryEmail = primaryEmail
            });
        }