using System; using System.Web; using AppLimit.CloudComputing.SharpBox.Common.Net.oAuth20; namespace AppLimit.CloudComputing.SharpBox.StorageProvider.SkyDrive.Authorization { /// /// Functions for requesting, refresh and maintain the access tokens for SkyDrive /// public static class SkyDriveAuthorizationHelper { /// /// Build uri string to obtain the authorization code /// /// ID of your app /// Uri string public static string BuildAuthCodeUrl(string clientID) { return BuildAuthCodeUrl(clientID, null); } /// /// Build uri string to obtain the authorization code /// /// ID of your app /// Scopes to which you want to get the access /// Uri string public static string BuildAuthCodeUrl(string clientID, string scopes) { return BuildAuthCodeUrl(clientID, scopes, null); } /// /// Build uri string to obtain the authorization code /// /// ID of your app /// Scopes to which you want to get the access /// Page to which you will be redirect with the authorization obtained code /// Uri string public static string BuildAuthCodeUrl(string clientID, string scopes, string redirectUri) { if (string.IsNullOrEmpty(clientID)) throw new ArgumentNullException("clientID"); if (string.IsNullOrEmpty(scopes)) scopes = SkyDriveConstants.DefaultScopes; if (string.IsNullOrEmpty(redirectUri)) redirectUri = SkyDriveConstants.DefaultRedirectUri; return string.Format("{0}?client_id={1}&scope={2}&response_type=code&redirect_uri={3}", SkyDriveConstants.OAuth20AuthUrl, HttpUtility.UrlEncode(clientID), HttpUtility.UrlEncode(scopes), redirectUri); } /// /// Exchange your authorization code for a valid access token /// /// ID of your app /// Secret of your app /// Authorization code /// Access token public static ICloudStorageAccessToken GetAccessToken(string clientID, string clientSecret, string authCode) { return GetAccessToken(clientID, clientSecret, null, authCode); } /// /// Exchange your authorization code for a valid access token /// /// ID of your app /// Secret of your app /// Redirect uri you used to obtained authorization code. Serves as request validator /// Authorization code /// Access token public static ICloudStorageAccessToken GetAccessToken(string clientID, string clientSecret, string redirectUri, string authCode) { if (string.IsNullOrEmpty(clientID)) throw new ArgumentNullException("clientID"); if (string.IsNullOrEmpty(clientSecret)) throw new ArgumentNullException("clientSecret"); if (string.IsNullOrEmpty(authCode)) throw new ArgumentNullException("authCode"); if (string.IsNullOrEmpty(redirectUri)) redirectUri = SkyDriveConstants.DefaultRedirectUri; var query = string.Format("client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&grant_type=authorization_code", clientID, redirectUri, clientSecret, authCode); var json = SkyDriveRequestHelper.PerformRequest(SkyDriveConstants.OAuth20TokenUrl, "application/x-www-form-urlencoded", "POST", query, 2); if (json != null) { var token = OAuth20Token.FromJson(json); token.ClientID = clientID; token.ClientSecret = clientSecret; token.RedirectUri = redirectUri; token.Timestamp = DateTime.UtcNow; return token; } return null; } /// /// Refresh expired access token /// /// Access token /// Refreshed access token public static ICloudStorageAccessToken RefreshToken(ICloudStorageAccessToken token) { var sdToken = token as OAuth20Token; if (sdToken == null || !CanRefresh(sdToken)) throw new ArgumentException("Can not refresh given token", "token"); var query = string.Format("client_id={0}&client_secret={1}&redirect_uri={2}&grant_type=refresh_token&refresh_token={3}", sdToken.ClientID, sdToken.ClientSecret, sdToken.RedirectUri, sdToken.RefreshToken); var json = SkyDriveRequestHelper.PerformRequest(SkyDriveConstants.OAuth20TokenUrl, "application/x-www-form-urlencoded", "POST", query, 2); if (json != null) { var refreshed = OAuth20Token.FromJson(json); refreshed.ClientID = sdToken.ClientID; refreshed.ClientSecret = sdToken.ClientSecret; refreshed.RedirectUri = sdToken.RedirectUri; refreshed.Timestamp = DateTime.UtcNow; return refreshed; } return token; } private static bool CanRefresh(OAuth20Token token) { return !string.IsNullOrEmpty(token.ClientID) && !string.IsNullOrEmpty(token.ClientSecret) && !string.IsNullOrEmpty(token.RedirectUri); } } }