public async Task <IActionResult> ViewMyChannels(ViewMyChannelsAddressModel model) { var appid = _tokenManager.ValidateAccessToken(model.AccessToken); var appLocal = await _dbContext.Apps.SingleOrDefaultAsync(t => t.Id == appid); if (appLocal == null) { appLocal = new StargateApp { Id = appid, Channels = new List <Channel>() }; _dbContext.Apps.Add(appLocal); await _dbContext.SaveChangesAsync(); } var channels = await _dbContext .Channels .Where(t => t.AppId == appid) .ToListAsync(); var viewModel = new ViewMyChannelsViewModel { AppId = appLocal.Id, Channels = channels .Select(t => new ChannelDetail(t, _connectedCountService.GetConnectedCount(t.Id), _lastAccessService.GetLastAccessTime(t.Id))) .ToList(), Code = ErrorType.Success, Message = "Successfully get your channels!" }; return(Json(viewModel)); }
public async Task <IActionResult> Log(LogAddressModel model) { var appid = _tokenManager.ValidateAccessToken(model.AccessToken); var appLocal = await _dbContext.StatusApps.SingleOrDefaultAsync(t => t.AppId == appid); if (appLocal == null) { appLocal = new StatusApp { AppId = appid }; _dbContext.StatusApps.Add(appLocal); await _dbContext.SaveChangesAsync(); } var newEvent = new ErrorLog { AppId = appid, Message = model.Message, StackTrace = model.StackTrace, EventLevel = model.EventLevel }; _dbContext.ErrorLogs.Add(newEvent); await _dbContext.SaveChangesAsync(); return(this.Protocol(ErrorType.Success, $"Successfully logged your event.")); }
public async Task <Folder> LocateSiteAndFolder(string accessToken, string siteName, string[] folderNames = null, bool recursiveCreate = false) { var appid = _tokenManager.ValidateAccessToken(accessToken); var site = await _dbContext .Sites .Include(t => t.Root) .Include(t => t.Root.SubFolders) .Include(t => t.Root.Files) .SingleOrDefaultAsync(t => t.SiteName.ToLower() == siteName.ToLower()); if (site == null) { throw new AiurAPIModelException(ErrorType.NotFound, "Not found target site!"); } if (site.AppId != appid) { throw new AiurAPIModelException(ErrorType.Unauthorized, "The target folder is not your app's folder!"); } if (folderNames == null || folderNames.Length == 0) { return(site.Root); } var folder = await LocateAsync(folderNames, site.Root, recursiveCreate); return(folder); }
public async Task <IActionResult> AllUserGranted([Required] string accessToken) { var appid = _tokenManager.ValidateAccessToken(accessToken); var grants = await _dbContext.LocalAppGrant.Include(t => t.User).Where(t => t.AppID == appid).Take(400).ToListAsync(); var model = new AllUserGrantedViewModel { AppId = appid, Grants = new List <Grant>(), Code = ErrorType.Success, Message = "Successfully get all your users" }; model.Grants.AddRange(grants); return(Json(model)); }
public async Task <IActionResult> PushMessage(PushMessageAddressModel model) { //Ensure app var appid = _tokenManager.ValidateAccessToken(model.AccessToken); //Ensure channel var channel = await _dbContext.Channels.SingleOrDefaultAsync(t => t.Id == model.ChannelId && t.AppId == appid); if (channel == null) { return(Json(new AiurProtocol { Code = ErrorType.NotFound, Message = "We can not find your channel!" })); } //Create Message var message = new Message { Id = _counter.GetUniqueNo, ChannelId = channel.Id, Content = model.MessageContent }; _memoryContext.Messages.Add(message); return(Json(new AiurProtocol { Code = ErrorType.Success, Message = $"You have successfully pushed a new message to channel: {channel.Id}!" })); }
public async Task <IActionResult> AllUserGranted(AllUserGrantedAddressModel model) { var appid = _tokenManager.ValidateAccessToken(model.AccessToken); var query = _dbContext .LocalAppGrant .Include(t => t.User) .Where(t => t.AppID == appid) .OrderByDescending(t => t.GrantTime); var result = await AiurPagedCollection <Grant> .BuildAsync( query, model, ErrorType.Success, "Successfully get all your users"); return(Json(result)); }
public async Task <IActionResult> Generate(GenerateAddressModel model) { var appid = _tokenManager.ValidateAccessToken(model.AccessToken); var file = await _dbContext .OSSFile .Include(t => t.BelongingBucket) .Where(t => t.BelongingBucket.BelongingAppId == appid) .SingleOrDefaultAsync(t => t.FileKey == model.Id); if (file == null) { return(this.Protocol(ErrorType.NotFound, "Could not get your file in your apps' buckets. The file may be out dated!")); } // Generate secret var newSecret = new Secret { Value = Guid.NewGuid().ToString("N"), FileId = file.FileKey, MaxUseTime = model.MaxUseTimes }; _dbContext.Secrets.Add(newSecret); await _dbContext.SaveChangesAsync(); return(Json(new AiurValue <string>(newSecret.Value) { Code = ErrorType.Success, Message = "Successfully created your onetime secret!" })); }
public async Task <IActionResult> PasswordAuth(PasswordAuthAddressModel model) { var appId = _tokenManager.ValidateAccessToken(model.AccessToken); var mail = await _dbContext .UserEmails .Include(t => t.Owner) .SingleOrDefaultAsync(t => t.EmailAddress == model.Email); if (mail == null) { return(this.Protocol(ErrorType.NotFound, $"The account with email {model.Email} was not found!")); } var user = mail.Owner; var result = await _signInManager.PasswordSignInAsync(user, model.Password, isPersistent : false, lockoutOnFailure : true); if (result.Succeeded) { if (!await user.HasAuthorizedApp(_dbContext, appId)) { await user.GrantTargetApp(_dbContext, appId); } var pack = await user.GeneratePack(_dbContext, appId); return(Json(new AiurValue <int>(pack.Code) { Code = ErrorType.Success, Message = "Auth success." })); } else if (result.RequiresTwoFactor) { throw new NotImplementedException(); } else if (result.IsLockedOut) { return(this.Protocol(ErrorType.Unauthorized, $"The account with email {model.Email} was locked! Please try again several minutes later!")); } else { return(this.Protocol(ErrorType.Unauthorized, "Wrong password!")); } }
public async Task <IActionResult> CreateNewSite(CreateNewSiteAddressModel model) { var appid = _tokenManager.ValidateAccessToken(model.AccessToken); var appLocal = await _dbContext.Apps.SingleOrDefaultAsync(t => t.AppId == appid); if (appLocal == null) { appLocal = new ProbeApp { AppId = appid }; _dbContext.Apps.Add(appLocal); await _dbContext.SaveChangesAsync(); } var conflict = await _dbContext.Sites .AnyAsync(t => t.SiteName.ToLower().Trim() == model.NewSiteName.ToLower().Trim()); if (conflict) { return(this.Protocol(ErrorType.NotEnoughResources, $"There is already a site with name: '{model.NewSiteName}'. Please try another new name.")); } var newRootFolder = new Folder { FolderName = "blob" }; _dbContext.Folders.Add(newRootFolder); await _dbContext.SaveChangesAsync(); var site = new Site { AppId = appid, SiteName = model.NewSiteName.ToLower(), FolderId = newRootFolder.Id }; _dbContext.Sites.Add(site); await _dbContext.SaveChangesAsync(); return(this.Protocol(ErrorType.Success, $"Successfully created your new site: '{site.SiteName}'.")); }
public async Task <IActionResult> CodeToOpenId(CodeToOpenIdAddressModel model) { var appId = _tokenManager.ValidateAccessToken(model.AccessToken); var targetPack = await _dbContext .OAuthPack //.Where(t => t.IsUsed == false) .SingleOrDefaultAsync(t => t.Code == model.Code); if (targetPack == null) { return(this.Protocol(ErrorType.WrongKey, "The code doesn't exists in our database.")); } // Use time is more than 10 seconds from now. if (targetPack.UseTime != DateTime.MinValue && targetPack.UseTime + new TimeSpan(0, 0, 0, 10) < DateTime.UtcNow) { return(this.Protocol(ErrorType.HasDoneAlready, "Code is used already!")); } if (targetPack.ApplyAppId != appId) { return(this.Protocol(ErrorType.Unauthorized, "The app granted code is not the app granting access token!")); } var capp = (await _apiService.AppInfoAsync(targetPack.ApplyAppId)).App; if (!capp.ViewOpenId) { return(this.Protocol(ErrorType.Unauthorized, "The app doesn't have view open id permission.")); } targetPack.UseTime = DateTime.UtcNow; await _dbContext.SaveChangesAsync(); var viewModel = new CodeToOpenIdViewModel { openid = targetPack.UserId, scope = "scope", Message = "Successfully get user openid", Code = ErrorType.Success }; return(Json(viewModel)); }
public async Task <GatewayUser> EnsureGranted(string accessToken, string userId, Func <App, bool> prefix) { var appid = _tokenManager.ValidateAccessToken(accessToken); var targetUser = await _dbContext.Users.Include(t => t.Emails).SingleOrDefaultAsync(t => t.Id == userId); var app = await _developerApiService.AppInfoAsync(appid); if (!_dbContext.LocalAppGrant.Any(t => t.AppID == appid && t.GatewayUserId == targetUser.Id)) { throw new AiurAPIModelException(ErrorType.Unauthorized, "This user did not grant your app!"); } if (prefix != null && !prefix(app.App)) { throw new AiurAPIModelException(ErrorType.Unauthorized, "You app is not allowed to do that!"); } return(targetUser); }
public async Task <JsonResult> DeleteApp(DeleteAppAddressModel model) { var appid = _tokenManager.ValidateAccessToken(model.AccessToken); if (appid != model.AppId) { return(this.Protocol(ErrorType.Unauthorized, "The app you try to delete is not the access token you granted!")); } var target = await _dbContext.Apps.FindAsync(appid); if (target != null) { _dbContext.OSSFile.RemoveRange(_dbContext.OSSFile.Include(t => t.BelongingBucket).Where(t => t.BelongingBucket.BelongingAppId == target.AppId)); _dbContext.Bucket.Delete(t => t.BelongingAppId == target.AppId); _dbContext.Apps.Remove(target); await _dbContext.SaveChangesAsync(); return(this.Protocol(ErrorType.Success, "Successfully deleted that app and all files.")); } return(this.Protocol(ErrorType.HasDoneAlready, "That app do not exists in our database.")); }
public async Task <IActionResult> GetUploadToken(GetUploadTokenAddressModel model) { var appid = _tokenManager.ValidateAccessToken(model.AccessToken); var site = await _dbContext .Sites .SingleOrDefaultAsync(t => t.SiteName == model.SiteName); if (site == null) { return(this.Protocol(ErrorType.NotFound, $"Could not find a site with name: '{model.SiteName}'")); } if (site.AppId != appid) { return(this.Protocol(ErrorType.Unauthorized, $"The site '{model.SiteName}' you tried to get a PBToken is not your app's site.")); } var(pbToken, deadline) = _pbTokenManager.GenerateAccessToken(site.SiteName, model.UnderPath, model.Permissions); return(Json(new AiurValue <string>(pbToken) { Code = ErrorType.Success, Message = $"Successfully get your PBToken! Use it before {deadline} UTC!" })); }