/// <summary> /// Adds or updates AuthToken for the particulat user and repository /// </summary> /// <param name="userAuthToken">AuthToken to be updated or added</param> /// <returns>AuthToken with updated values</returns> public AuthToken AddUpdateAuthToken(AuthToken userAuthToken) { AuthToken result; if (userAuthToken.Id > 0 ) { result = this.userRepository.UpdateAuthToken(userAuthToken); } else { // check if the auth token exists for the user and repository. AuthToken token = this.GetUserAuthToken(userAuthToken.UserId, userAuthToken.RespositoryId); if (token != null) { userAuthToken.Id = token.Id; result = this.userRepository.UpdateAuthToken(userAuthToken); } else { result = this.userRepository.AddAuthToken(userAuthToken); } } unitOfWork.Commit(); return result; }
public void Allow_Publish_For_Impersonated_Repository() { // construct the model // construct the model AuthToken authToken = new AuthToken() { UserId = 1, RespositoryId = 2 }; File fakeFile = new File() { Name = "test1", FileId = 100, RepositoryId = 1, CreatedBy = 1, Status = FileStatus.None.ToString() }; PublishMessage model = new PublishMessage() { RepositoryId = 1, AuthToken = authToken, FileId = fakeFile.FileId, UserId = 1 }; this.repository = new Repository() { BaseRepositoryId = 2, Name = "test", IsImpersonating = true, AccessToken = "accessToken", RefreshToken = "refreshToken", TokenExpiresOn = DateTime.UtcNow.AddHours(1), BaseRepository = new BaseRepository { Name = "SkyDrive" } }; SkyDriveFileService skyDriveFileService = this.GetSkyDriveFileService(); IFileProcesser fileProcessor = new StubIFileProcesser() { DownloadDocumentFile = file => new DataDetail() { FileNameToDownLoad = "abc.xyz" } }; string fileIdentifier; using (ShimsContext.Create()) { ShimFileFactory.GetFileTypeInstanceStringIBlobDataRepositoryIFileRepositoryIRepositoryService = (fileExtension, blobDataRepository, fileDataRepository, repositoryService) => fileProcessor; fileIdentifier = skyDriveFileService.PublishFile(model); } Assert.IsFalse(string.IsNullOrEmpty(fileIdentifier)); }
public HttpResponseMessage AddUpdateAuthToken(AuthToken token) { try { // Create file service instance Repository repository = this.repositoryService.GetRepositoryById(token.RespositoryId); if (null == repository) { diagnostics.WriteErrorTrace(TraceEventId.Exception, "Repository is null"); return Request.CreateErrorResponse(HttpStatusCode.NotFound, MessageStrings.Invalid_Repository_id); } token.UserId = this.user.UserId; this.userService.AddUpdateAuthToken(token); return Request.CreateResponse(HttpStatusCode.OK); } catch (ArgumentException ex) { message = string.Format(CultureInfo.CurrentCulture, MessageStrings.Argument_Error_Message_Template, ex.ParamName); status = HttpStatusCode.BadRequest; } return Request.CreateErrorResponse(status, message); }
/// <summary> /// Downloads the file to a temp location /// </summary> /// <param name="fileId">File Id</param> /// <param name="fileName">File Name</param> /// <param name="tempFolderPath">Temp folder path</param> /// <param name="token">AuthToken</param> /// <returns></returns> private OperationStatus DownloadFile(string fileId,string fileName, string tempFolderPath, AuthToken token) { OperationStatus status; try { HttpWebRequest request = WebRequest.Create(string.Format("{0}/{1}/content?access_token={2}", skydriveBaseUrl, fileId, token.AccessToken)) as HttpWebRequest; request.Method = "GET"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { string filePath = string.Format("{0}/{1}", tempFolderPath, fileName); using (FileStream fs = new FileStream(filePath, FileMode.Create)) { using (Stream s = response.GetResponseStream()) { byte[] read = new byte[256]; int count = s.Read(read, 0, read.Length); while (count > 0) { fs.Write(read, 0, count); count = s.Read(read, 0, read.Length); } } } } status = OperationStatus.CreateSuccessStatus(); } catch (Exception exception) { diagnostics.WriteErrorTrace(TraceEventId.Exception, exception); status = OperationStatus.CreateFailureStatus(exception); } return status; }
/// <summary> /// The method creates the folder structure to upload the file /// </summary> /// <param name="repositoryName">Repository Name</param> /// <param name="fileId">File Id</param> /// <param name="authToken">Auth Token</param> /// <returns>Folder Id</returns> private string CreateFolderForFile(string repositoryName, string fileId, string fileName, AuthToken authToken) { string parentFolderId = GetRootFolder(authToken); diagnostics.WriteVerboseTrace(TraceEventId.Flow, string.Format("Root Folder {0}", parentFolderId)); // Check if the Folder with name as Repostory exists if not then create a folder with repository name string repositoryFolderId = this.GetOrCreateFolder(repositoryName, parentFolderId, authToken); //Check if the folder for current date exists. string datefolderName = DateTime.UtcNow.ToString("yyyy-MM-dd"); string dateFolderId = this.GetOrCreateFolder(datefolderName, repositoryFolderId, authToken); //Check if the folder with the name as File Id exists if not then create string fileFolderName = string.Format("{0}_{1}", fileName, fileId); string fileFolderId = this.GetOrCreateFolder(fileFolderName, dateFolderId, authToken); // return the folder Id. the file will be uploaded to this folder. return fileFolderId; }
/// <summary> /// Creates the folder. /// </summary> /// <param name="folderName">Folder Name.</param> /// <param name="parentFolderId">Parent Folder Id.</param> /// <param name="token">AuthToken instance.</param> /// <returns>Folder Id.</returns> private string CreateFolder(string folderName, string parentFolderId, AuthToken token) { HttpWebRequest request = WebRequest.Create(string.Format("{0}/{1}", skydriveBaseUrl, parentFolderId)) as HttpWebRequest; request.Headers.Add("Authorization", string.Format("Bearer {0}",HttpUtility.UrlEncode(token.AccessToken))); request.Method = "POST"; request.ContentType = "application/json;boundary=" + skyDriveMultiPartRequestBoundary; Dictionary<string, string> data = new Dictionary<string, string>(); data.Add("name", folderName); JavaScriptSerializer serializer = new JavaScriptSerializer(); string postData = serializer.Serialize(data); using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(postData); } using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { DataContractJsonSerializer dataContractSerializer = new DataContractJsonSerializer(typeof(Content)); Content content = dataContractSerializer.ReadObject(response.GetResponseStream()) as Content; if (content != null) { return content.Id; } } return null; }
/// <summary> /// Retrives the Access token by passing refresh token /// </summary> /// <param name="refreshToken">refresh Token</param> /// <returns></returns> public AuthToken RefreshToken(string refreshToken) { string baseRepositoryName = BaseRepositoryEnum.SkyDrive.ToString(); string url = ConfigReader<string>.GetRepositoryConfigValues(baseRepositoryName, SkyDriveConstants.OAuthUrl); string clientId = ConfigReader<string>.GetRepositoryConfigValues(baseRepositoryName, SkyDriveConstants.ClientId); string redirectionUrl = ConfigReader<string>.GetRepositoryConfigValues(baseRepositoryName, SkyDriveConstants.RedicrectionUrl); string clientSecret = ConfigReader<string>.GetRepositoryConfigValues(baseRepositoryName, SkyDriveConstants.ClientSecret); HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; string content = String.Format("client_id={0}&redirect_uri={1}&client_secret={2}&refresh_token={3}&grant_type=refresh_token", HttpUtility.UrlEncode(clientId), HttpUtility.UrlEncode(redirectionUrl), HttpUtility.UrlEncode(clientSecret), HttpUtility.UrlEncode(refreshToken)); using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(content); } HttpWebResponse response = request.GetResponse() as HttpWebResponse; DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(OAuthToken)); OAuthToken oAuthToken = serializer.ReadObject(response.GetResponseStream()) as OAuthToken; int expiresInSeconds = Convert.ToInt32(oAuthToken.ExpiresIn); AuthToken token = new AuthToken() { AccessToken = oAuthToken.AccessToken, RefreshToken = oAuthToken.RefreshToken, TokenExpiresOn = DateTime.UtcNow.AddSeconds(expiresInSeconds) }; return token; }
/// <summary> /// Adds the AuthToken /// </summary> /// <param name="authToken">AuthToken</param> /// <returns>AuthToken</returns> public AuthToken AddAuthToken(AuthToken authToken) { Check.IsNotNull<AuthToken>(authToken, "new UserAuthToken"); return Context.AuthTokens.Add(authToken); }
/// <summary> /// Gets the Root Folder. /// </summary> /// <param name="token">AuthToken Instance.</param> /// <returns>Folder Id.</returns> private string GetRootFolder(AuthToken token) { HttpWebRequest request = WebRequest.Create(string.Format("{0}?access_token={1}", rootFolderUrl, token.AccessToken)) as HttpWebRequest; request.Method = "GET"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Content)); Content rootFolder = serializer.ReadObject(response.GetResponseStream()) as Content; return rootFolder.Id; } }
/// <summary> /// Returns the Folder Id. /// </summary> /// <param name="folderName">Folder Id.</param> /// <param name="parentFolderId">Parent Folder Id.</param> /// <param name="token">AuthToken instance.</param> /// <returns>Folder Id.</returns> private string GetFolder(string folderName, string parentFolderId, AuthToken token ) { Structure structure = this.GetChildFolders(parentFolderId, token); Content folder = structure.Items.Where(i => i.Name.ToLowerInvariant() == folderName.ToLowerInvariant()).FirstOrDefault(); if (folder != null) { return folder.Id; } return null; }
/// <summary> /// Downloads the File from Repository /// </summary> /// <param name="file">File object.</param> /// <param name="repository">Repository instance.</param> /// <param name="user">User instance.</param> /// <param name="credentials">credentials required by the repository.</param> /// <returns>DataFile containing the file data.</returns> public override DataFile DownLoadFileFromRepository(File file, Repository repository, User user, RepositoryCredentials credentials) { // construct the AuthToken object AuthToken authToken = new AuthToken() { UserId = user.UserId, RespositoryId = repository.RepositoryId }; if (credentials != null) { authToken.AccessToken = credentials[WindowsLiveAuthenticationCredentialKeys.AccessToken]; authToken.RefreshToken = credentials[WindowsLiveAuthenticationCredentialKeys.RefreshToken]; if (credentials[WindowsLiveAuthenticationCredentialKeys.TokenExpiresOn] != null) { DateTime tokenExpiryDate; if (DateTime.TryParse(credentials[WindowsLiveAuthenticationCredentialKeys.TokenExpiresOn], out tokenExpiryDate)) { authToken.TokenExpiresOn = tokenExpiryDate; }; } } this.RepositoryAdapter = this.RepositoryAdapterFactory.GetRepositoryAdapter(repository.BaseRepository.Name); // Retreive the AuthToken from database or save the token to database if access token is present in the credentials. authToken = this.GetOrUpdateAuthTokens(repository, authToken); DataFile dataFile = this.RepositoryAdapter.DownloadFile(file.Identifier, authToken.AccessToken, file.Name); return dataFile; }
/// <summary> /// Returns the instance of SkyDriveFile Service /// </summary> /// <returns>SkyDriveFileService</returns> private SkyDriveFileService GetSkyDriveFileService() { this.repositoryService = new StubIRepositoryService() { GetRepositoryByIdInt32 = (repositoryId) => { return this.repository; }, }; this.userService = new StubIUserService() { GetUserAuthTokenInt32Int32 = (userId, RepositoryId) => { return this.userAuthToken; }, AddUpdateAuthTokenAuthToken = authToken => { return authToken; } }; this.fileRepository = new StubIFileRepository() { GetItemInt32Int32 = (userId, fileName) => { return new File() { Name = "test", FileId = 100, Status = "Uploaded" }; }, UpdateFileFile = (file) => { return file; } }; IUnitOfWork unitOfWork = new Fakes.StubIUnitOfWork() { Commit = () => { return; } }; this.blobDataRepository = new StubIBlobDataRepository() { GetBlobContentString = (name) => { return new DataDetail(); }, DeleteFileString = (fileName) => { return true; } }; this.skyDriveAdapter = new StubIRepositoryAdapter() { PostFilePublishFileModel = (publishFileModel) => { OperationStatus status = OperationStatus.CreateSuccessStatus(); status.CustomReturnValues = "x1234"; return status; }, RefreshTokenString = (accessToken) => { AuthToken authToken = new AuthToken() { AccessToken = "accessToken", RefreshToken = "refreshToken", TokenExpiresOn = DateTime.UtcNow.AddHours(1) }; return authToken; } }; IRepositoryAdapterFactory adapterFactory = new StubIRepositoryAdapterFactory() { GetRepositoryAdapterString = (baseRepositoryName) => { return skyDriveAdapter; } }; this.repositoryDetails = new StubIRepositoryDetails() { GetRepositoryByIdInt32 = id => new Repository() { BaseRepository = new BaseRepository() { Name = "Repository Type 1" } } }; SkyDriveFileService skyDriveFileService; using (ShimsContext.Create()) { ShimDiagnosticsProvider.ConstructorType = (dp, type) => { }; skyDriveFileService = new SkyDriveFileService(fileRepository, blobDataRepository, unitOfWork, repositoryDetails, repositoryService, userService, adapterFactory); } return skyDriveFileService; }
public void Return_ErrorCode_When_Adapter_Returns_Failure() { // construct the model AuthToken authToken = new AuthToken() { AccessToken = "accessToken", RefreshToken = "refreshToken", TokenExpiresOn = DateTime.UtcNow.AddHours(-1), RespositoryId = 1 }; File fakeFile = new File() { Name = "test1", FileId = 100, RepositoryId = 1, CreatedBy = 1, Status = FileStatus.None.ToString() }; PublishMessage model = new PublishMessage() { RepositoryId = 3, AuthToken = authToken, FileId = fakeFile.FileId, UserId = 1 }; this.repository = new Repository() { BaseRepositoryId = 2, Name = "test", IsImpersonating = false, BaseRepository = new BaseRepository { Name = "SkyDrive" } }; this.userAuthToken = null; var fileProvider = this.GetSkyDriveFileService(); this.skyDriveAdapter = new Microsoft.Research.DataOnboarding.RepositoryAdapters.Interfaces.Fakes.StubIRepositoryAdapter() { PostFilePublishFileModel = (publishFileModel) => { OperationStatus status = OperationStatus.CreateFailureStatus("error"); return status; }, }; IFileProcesser fileProcessor = new StubIFileProcesser() { DownloadDocumentFile = file => new DataDetail() { FileNameToDownLoad = "abc.xyz" } }; string fileIdentifier; using (ShimsContext.Create()) { ShimFileFactory.GetFileTypeInstanceStringIBlobDataRepositoryIFileRepositoryIRepositoryService = (fileExtension, blobDataRepository, fileDataRepository, repositoryService) => fileProcessor; ShimSkyDriveFileService.AllInstances.GetOrUpdateAuthTokensRepositoryAuthToken = (skyDriveFileService, repository, at) => new AuthToken(); fileIdentifier = fileProvider.PublishFile(model); } Assert.IsTrue(string.IsNullOrEmpty(fileIdentifier)); }
/// <summary> /// Downloads the file and returns the byte array /// </summary> /// <param name="fileId">Skydrive File Id</param> /// <param name="token">AuthToken</param> /// <returns>Byte array</returns> private byte[] DownloadFile(string fileId, AuthToken token) { byte[] fileContent ; HttpWebRequest request = WebRequest.Create(string.Format("{0}/{1}/content?access_token={2}", skydriveBaseUrl, fileId, token.AccessToken)) as HttpWebRequest; request.Method = "GET"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { using (MemoryStream memoryStream = new MemoryStream()) { using (Stream stream = response.GetResponseStream()) { byte[] read = new byte[256]; int count = stream.Read(read, 0, read.Length); while (count > 0) { memoryStream.Write(read, 0, count); count = stream.Read(read, 0, read.Length); } } fileContent = memoryStream.ToArray(); } } return fileContent; }
/// <summary> /// Gets or updates the AuthTokens /// </summary> /// <param name="repository">Repository</param> /// <param name="userAuthToken">AuthToken</param> /// <returns>AuthToken</returns> private AuthToken GetOrUpdateAuthTokens(Repository repository, AuthToken userAuthToken) { AuthToken token = userAuthToken; // if Impersonating then get the token from repository if ((bool)repository.IsImpersonating) { diagnostics.WriteVerboseTrace(TraceEventId.Flow, "Impersonation is ON"); token = new AuthToken() { AccessToken = repository.AccessToken, RefreshToken = repository.RefreshToken, TokenExpiresOn = (DateTime)repository.TokenExpiresOn }; } // if accessToken is null then get the authtoken from the database else { if (string.IsNullOrEmpty(userAuthToken.AccessToken)) { diagnostics.WriteVerboseTrace(TraceEventId.Flow, "Get the token from the database"); token = this.userService.GetUserAuthToken(userAuthToken.UserId, repository.RepositoryId); } else if(userAuthToken.Id <= 0) { diagnostics.WriteVerboseTrace(TraceEventId.Flow, "Request has the token so add the token to database"); token = this.userService.AddUpdateAuthToken(userAuthToken); } } if (null == token) { throw new AccessTokenNotFoundException() { RepositoryId = repository.RepositoryId, UserId = userAuthToken.UserId }; } // check if the AuthToken expired if yes then get new access token from refresh token return RefreshAccessToken(repository, token); }
/// <summary> /// Returns the list of child folder. /// </summary> /// <param name="parentFolderId">Parent Folder Id.</param> /// <param name="token">AuthToken instance.</param> /// <returns>Strucure Instance.</returns> private Structure GetChildFolders(string parentFolderId, AuthToken token) { HttpWebRequest request = WebRequest.Create(string.Format("{0}/{1}/files?access_token={2}", skydriveBaseUrl, parentFolderId, token.AccessToken)) as HttpWebRequest; request.Method = "GET"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Structure)); Structure structure = serializer.ReadObject(response.GetResponseStream()) as Structure; return structure; } }
/// <summary> /// Returns the AuthToken for the Repository and User /// </summary> /// <param name="repository">Repository instance.</param> /// <param name="userId">User Id.</param> /// <returns>AuthToken object.</returns> private AuthToken GetAuthTokens(Repository repository, int userId) { AuthToken token; // if Impersonating then get the token from repository if ((bool)repository.IsImpersonating) { diagnostics.WriteVerboseTrace(TraceEventId.Flow, "Impersonation is ON"); token = new AuthToken() { AccessToken = repository.AccessToken, RefreshToken = repository.RefreshToken, TokenExpiresOn = (DateTime)repository.TokenExpiresOn }; } else { diagnostics.WriteVerboseTrace(TraceEventId.Flow, "Get the token from the database"); token = this.userService.GetUserAuthToken(userId, repository.RepositoryId); } if (null == token) { throw new AccessTokenNotFoundException() { RepositoryId = repository.RepositoryId, UserId = userId }; } // check if the AuthToken expired if yes then get new access token from refresh token return RefreshAccessToken(repository, token); }
/// <summary> /// Checks if the folder exists and if not then creates /// </summary> /// <param name="folderName">Folder to be created</param> /// <param name="parentFolderId">Parent folder Id</param> /// <param name="authToken">Auth Token</param> /// <returns>Folder Id</returns> private string GetOrCreateFolder(string folderName, string parentFolderId, AuthToken authToken) { string folderId = GetFolder(folderName, parentFolderId, authToken); if (string.IsNullOrEmpty(folderId)) { folderId = CreateFolder(folderName, parentFolderId, authToken); diagnostics.WriteVerboseTrace(TraceEventId.Flow, string.Format("Folder {0} created", folderName)); } return folderId; }
/// <summary> /// validates if the token is expired /// </summary> /// <param name="token">AuthToken</param> /// <returns>Boolean</returns> private AuthToken RefreshAccessToken(Repository repository, AuthToken token) { if (DateTime.UtcNow < token.TokenExpiresOn) { return token; } AuthToken freshToken = this.RepositoryAdapter.RefreshToken(token.RefreshToken); freshToken.RespositoryId = token.RespositoryId; freshToken.UserId = token.UserId; if ((bool)repository.IsImpersonating) { repository.AccessToken = freshToken.AccessToken; repository.RefreshToken = freshToken.RefreshToken; repository.TokenExpiresOn = freshToken.TokenExpiresOn; this.RepositoryDetails.UpdateRepository(repository); } else { this.userService.AddUpdateAuthToken(freshToken); } return freshToken; }
/// <summary> /// Method to download the file from the repository. /// </summary> /// <param name="downloadInput">Input data required to download the file from the repository.</param> /// <returns>Downloaded file data.</returns> public DataFile DownloadFile(string downloadUrl, string authorization, string fileName) { DataFile dataFile = new DataFile(); AuthToken token = new AuthToken() { AccessToken = authorization }; dataFile.FileContent = this.DownloadFile(downloadUrl, token); string extension = Path.GetExtension(fileName); if (extension.ToUpperInvariant().Equals(Constants.XLSX, StringComparison.OrdinalIgnoreCase)) { dataFile.FileExtentsion = extension; } else { dataFile.FileExtentsion = ".zip"; } dataFile.FileName = string.Format("{0}{1}", Path.GetFileNameWithoutExtension(fileName), dataFile.FileExtentsion); return dataFile; }
/// <summary> /// Updates Auth token for the User /// </summary> /// <param name="userAuthToken">AuthToken</param> /// <returns>AuthToken</returns> public AuthToken UpdateAuthToken(AuthToken userAuthToken) { Check.IsNotNull<AuthToken>(userAuthToken, "modified UserAuthToken"); AuthToken originalAuthToken = Context.AuthTokens.Where(authToken => authToken.Id == userAuthToken.Id).FirstOrDefault(); // get the entry using the originalAuthtoken var entry = Context.GetEntry<AuthToken>(originalAuthToken); entry.CurrentValues.SetValues(userAuthToken); return userAuthToken; }