示例#1
0
 private async Task<bool> RefreshTokenIfNeed()
 {
     if (IsExpired(token))
     {
         token = await KBApiUtil.RefreshTokenAsync(token.RefreshToken);
         FileUtil.writeTokenFile(token);
     }
     return true;
 }
示例#2
0
 // Check current time is exceed expiresTime in token or not.
 private bool IsExpired(KbToken token)
 {
     DateTime expiresTime = DateTime.Parse(token.ExpiresTime);
     DateTime nowTime = DateTime.Now;
     return nowTime > expiresTime;
 }
示例#3
0
 private static void SetHeaderAuthorization(WebClient webclient, KbToken token)
 {
     webclient.Headers["Authorization"] = KB_DEFAULT_TOKENTYPE + " " + token.AccessToken;
 }
示例#4
0
 // Make sure get token and it hasn't expired.
 private async Task<bool> CheckToken()
 {
     if (token == null)
     {
         token = FileUtil.readTokenFile();
     }
     if (token == null)
     {
         (App.Current as App).RootFrame.Navigate(new Uri("/AuthPage.xaml", UriKind.Relative));
         return false;
     }
     await RefreshTokenIfNeed();
     return true;
 }
示例#5
0
 private static void UpdateExpiresTime(KbToken token)
 {
     token.ExpiresTime = DateTime.Now.AddSeconds(token.ExpiresIn).ToString();
 }
示例#6
0
 public static void GetFileList(string path, KbToken token, DownloadStringCompletedEventHandler callback)
 {
     Uri pathUrl = new Uri(ListUrl + path);
     WebClient webclient = new WebClient();
     SetHeaderAuthorization(webclient, token);
     webclient.DownloadStringCompleted += callback;
     webclient.DownloadStringAsync(pathUrl);
 }
示例#7
0
 private static Dictionary<string, string> GetAuthorizationHeader(KbToken token)
 {
     Dictionary<string, string> header = new Dictionary<string, string>();
     header.Add("Authorization", KB_DEFAULT_TOKENTYPE + " " + token.AccessToken);
     return header;
 }
示例#8
0
 /// <summary>
 /// Download file from server.
 /// </summary>
 /// <param name="path"></param>
 /// <returns></returns>
 public static async Task<StorageFile> DownloadFileAsync(string path, KbToken token)
 {
     string url = DownloadUrl + Uri.EscapeDataString(path);
     Stream respStream = await doGetBytesAsync(url, GetAuthorizationHeader(token));
     StorageFile sfile = await FileUtil.SaveStream(respStream, path);
     respStream.Close();
     return sfile;
 }
示例#9
0
 /// <summary>
 /// Get account info from server.
 /// </summary>
 /// <param name="token"></param>
 /// <returns></returns>
 public static async Task<KbAccountInfo> GetAccountInfo(KbToken token)
 {
     string received = await doGetAsync(InfoUrl, GetAuthorizationHeader(token));
     KbAccountInfo accountInfo = await JsonConvert.DeserializeObjectAsync<KbAccountInfo>(received);
     return accountInfo;
 }
示例#10
0
 /// <summary>
 /// Get file list in special path and check the hashcode to see if content was changed.
 /// </summary>
 /// <param name="path"></param>
 /// <param name="token"></param>
 /// <param name="hashcode"></param>
 /// <returns></returns>
 public static async Task<KbListInfo> GetFileListAsync(string path, KbToken token, string hashcode)
 {
     string url = ListUrl + path;
     if (hashcode != null)
     {
         url += "?hash=" + hashcode;
     }
     string received = await doGetAsync(url, GetAuthorizationHeader(token));
     KbListInfo listInfo = await JsonConvert.DeserializeObjectAsync<KbListInfo>(received);
     return listInfo;
 }
示例#11
0
 /// <summary>
 /// Get file list in special path.
 /// </summary>
 /// <param name="path"></param>
 /// <param name="token"></param>
 /// <returns></returns>
 public static Task<KbListInfo> GetFileListAsync(string path, KbToken token)
 {
     return GetFileListAsync(path, token, null);
 }