Example #1
0
        /// <summary>
        /// Perform request to obtain access token by code or by refresh token
        /// </summary>
        /// <param name="postData">Complete POST data for the request</param>
        /// <returns></returns>
        private static LoginResponse RequestToken(string postData)
        {
            var request = new ProxiedWebRequest(tokenUrl);

            request.UserAgent = "MCC/" + Program.Version;
            var response = request.Post("application/x-www-form-urlencoded", postData);
            var jsonData = Json.ParseJson(response.Body);

            // Error handling
            if (jsonData.Properties.ContainsKey("error"))
            {
                throw new Exception(jsonData.Properties["error_description"].StringValue);
            }
            else
            {
                string accessToken  = jsonData.Properties["access_token"].StringValue;
                string refreshToken = jsonData.Properties["refresh_token"].StringValue;
                int    expiresIn    = int.Parse(jsonData.Properties["expires_in"].StringValue);

                // Extract email from JWT
                string payload     = JwtPayloadDecode.GetPayload(jsonData.Properties["id_token"].StringValue);
                var    jsonPayload = Json.ParseJson(payload);
                string email       = jsonPayload.Properties["email"].StringValue;
                return(new LoginResponse()
                {
                    Email = email,
                    AccessToken = accessToken,
                    RefreshToken = refreshToken,
                    ExpiresIn = expiresIn
                });
            }
        }
Example #2
0
        /// <summary>
        /// Validates whether accessToken must be refreshed
        /// </summary>
        /// <param name="session">Session token to validate</param>
        /// <returns>Returns the status of the token (Valid, Invalid, etc.)</returns>
        public static LoginResult GetTokenValidation(SessionToken session)
        {
            var payload      = JwtPayloadDecode.GetPayload(session.ID);
            var json         = Json.ParseJson(payload);
            var expTimestamp = long.Parse(json.Properties["exp"].StringValue);
            var now          = DateTime.Now;
            var tokenExp     = UnixTimeStampToDateTime(expTimestamp);

            if (Settings.DebugMessages)
            {
                ConsoleIO.WriteLine("Access token expiration time is " + tokenExp.ToString());
            }
            if (now < tokenExp)
            {
                // Still valid
                return(LoginResult.Success);
            }
            else
            {
                // Token expired
                return(LoginResult.LoginRequired);
            }
        }