Ejemplo n.º 1
0
        public IAuthenticatedHttpClient MakeAuthenticatedHttpClientWithCustomCredentials(TentPost<object> credentialsPost)
        {
            var http = this.serviceProvider.Resolve<IAuthenticatedHttpClient>();
            http.SetCredentials(credentialsPost);

            return http;
        }
Ejemplo n.º 2
0
        public Task UpdateAsync(string ownerId, TentPost post, bool isFromFollowing, CancellationToken cancellationToken = new CancellationToken())
        {
            // Create a new UserPost from the provided post.
            var userPost = this.userPostFactory.FromPost(ownerId, post, isFromFollowing);

            return this.db.Run(async c =>
            {
                // Set the key values.
                userPost.KeyOwnerUserPost = null;
                userPost.KeyOwnerUserPostVersion = new [] { ownerId, userPost.UserId, userPost.PostId, this.modelHelpers.GetShortVersionId(userPost.VersionId) };

                // Start by saving this specific version.
                var versionInsertResult = await this.tableVersions
                    .Insert(userPost)
                    .RunResultAsync(c, null, cancellationToken);

                versionInsertResult.AssertInserted(1);

                // Set the key values.
                userPost.KeyOwnerUserPost = new [] { ownerId, post.UserId, post.Id };
                userPost.KeyOwnerUserPostVersion = null;

                // Then, conditionally update the last version.
                var upsertResult = await this.table
                    .Get(userPost.KeyOwnerUserPost)
                    .Replace(r => this.db.R.Branch(r.Eq(null)
                        .Or(r.HasFields("deleted_at"))
                        .Or(r.G("version_received_at").Lt(userPost.VersionReceivedAt)
                            .Or(r.G("version_received_at").Eq(userPost.VersionReceivedAt).And(r.G("version").Lt(userPost.VersionId)))),
                        userPost, r))
                    .RunResultAsync(c, null, cancellationToken);

                upsertResult.AssertNoErrors();
            }, cancellationToken);
        }
Ejemplo n.º 3
0
 public ITentRequestPost FromPost(TentPost post)
 {
     Ensure.Argument.IsNotNull(post, nameof(post));
     return new TentRequestPost(this.userLogic, this.userRepository, this.postRepository, this.uriHelpers)
     {
         UserId = post.UserId,
         Post = post
     };
 }
Ejemplo n.º 4
0
 public ITentClient Make(TentPost<TentContentMeta> target)
 {
     return new TentClient(
         this.httpRequestFactory,
         this.httpClientFactory,
         this.queryStringHelpers,
         this.bewitLogic,
         this.uriHelpers,
         this.tentConstants,
         target);
 }
Ejemplo n.º 5
0
 public ITentClient MakeAuthenticated(TentPost<TentContentMeta> target, ITentHawkSignature credentials)
 {
     return new TentClient(
         this.httpRequestFactory,
         this.httpClientFactory,
         this.queryStringHelpers,
         this.bewitLogic,
         this.uriHelpers,
         this.tentConstants,
         target,
         credentials);
 }
Ejemplo n.º 6
0
        public async Task<string> CreateBewitForPostAsync(User user, TentPost post, TimeSpan expiresIn, CancellationToken cancellationToken = default(CancellationToken))
        {            
            // Compute the expiration date for this bewit.
            var expiresAt = DateTime.UtcNow + expiresIn;

            // Create the bewit object and save it.
            var bewit = this.bewitFactory.FromExpirationDate(expiresAt);
            await this.bewitRepository.UpdateAsync(bewit, cancellationToken);

            // Generate the bewit signature.
            return this.cryptoHelpers.CreateBewit(expiresAt, this.uriHelpers.GetCamprPostUri(user.Handle, post.Id), null, bewit.Id, bewit.Key);

        }
Ejemplo n.º 7
0
 public UserPost FromPost(string ownerId, TentPost post, bool isFromFollowing)
 {
     return new UserPost
     {
         OwnerId = ownerId,
         UserId = post.UserId,
         PostId = post.Id,
         VersionId = post.Version.Id,
         IsFromFollowing = isFromFollowing,
         VersionReceivedAt = post.Version.ReceivedAt.GetValueOrDefault(),
         VersionPublishedAt = post.Version.PublishedAt.GetValueOrDefault(),
         ReceivedAt = post.ReceivedAt.GetValueOrDefault(),
         PublishedAt = post.PublishedAt.GetValueOrDefault(),
         Type = post.Type,
         Mentions = post.Mentions?.Select(this.BuildMention).ToList(),
         Permissions = this.BuildPermissions(post.Permissions)
     };
 }
Ejemplo n.º 8
0
        public async Task Resolve(CancellationToken cancellationToken = default(CancellationToken))
        {
            // If we don't have a User, retrieve it.
            if (this.User == null)
            {
                // If an entity was provided, use it to retrieve the corresponding user.
                if (!string.IsNullOrWhiteSpace(this.Entity))
                    this.User = await this.userLogic.GetUserAsync(this.Entity, cancellationToken);
                // Otherwise, use the User Id, if any.
                else if (!string.IsNullOrWhiteSpace(this.UserId))
                    this.User = await this.userRepository.GetAsync(this.UserId, cancellationToken);
            }

            // If we're missing a user at this point, throw.
            if (this.User == null)
                throw new Exception("Couldn't resolve the User for this Request Post.");

            // If we don't have a post, retrieve it.
            if (this.Post == null && !string.IsNullOrWhiteSpace(this.PostId))
                this.Post = await this.postRepository.GetAsync<object>(this.User.Id, this.PostId, cancellationToken);
        }
Ejemplo n.º 9
0
        public async Task<ITentHawkSignature> GetCredentialsForUserAsync(User user, User targetUser, bool createIfNotFound, TentPost<TentContentCredentials> credentials, CancellationToken cancellationToken = new CancellationToken())
        {
            Ensure.Argument.IsNotNull(user, nameof(user));
            Ensure.Argument.IsNotNull(targetUser, nameof(targetUser));

            // If a credentials post was provided, use it to create the signature.
            if (credentials != null)
                return this.hawkSignatureFactory.FromCredentials(credentials);

            // Otherwise, retrieve the relationship between our two users.
            var localRelationshipPost = await this.GetRelationship(user, targetUser, createIfNotFound, cancellationToken);
            if (localRelationshipPost?.Mentions == null || !localRelationshipPost.Mentions.Any(m => m.UserId != user.Id && m.FoundPost))
                return null;

            // Extract the mention to the remote relationship post.
            var remoteRelationshipPostMention = localRelationshipPost.Mentions.First(m =>
                m.UserId != user.Id && m.FoundPost);

            // Retrieve the credentials post for the remote relationship post.
            var remoteCredentialsPost = await this.postLogic.GetLastPostOfTypeMentioningAsync<TentContentCredentials>(user, user, targetUser,
                this.tentConstants.CredentialsPostType,
                this.requestPostFactory.FromMention(remoteRelationshipPostMention),
                cancellationToken);

            return remoteCredentialsPost == null
                ? null
                : this.hawkSignatureFactory.FromCredentials(remoteCredentialsPost);
        }
Ejemplo n.º 10
0
        public Task DeleteAsync(string ownerId, TentPost post, bool specificVersion, CancellationToken cancellationToken = new CancellationToken())
        {
            return this.db.Run(async c =>
            {
                var deletedAt = new { deletedAt = post.DeletedAt.GetValueOrDefault() };

                // If a specific version was specified, retrieve the penultimate version.
                var postVersionReceivedAtIndex = "owner_user_post_versionreceivedat";
                var penultimatePostVersion = !specificVersion
                    ? null
                    : await this.tableVersions
                        .Between(
                            new object[] { ownerId, post.UserId, post.Id, this.db.R.Minval() },
                            new object[] { ownerId, post.UserId, post.Id, this.db.R.Maxval() })[new { index = postVersionReceivedAtIndex }]
                        .OrderBy()[new { index = this.db.R.Desc(postVersionReceivedAtIndex) }]
                        .Nth(1)
                        .Default_((object)null)
                        .RunResultAsync<UserPost>(c, null, cancellationToken);

                // If a penultimate version was found, prepare for insertion in different table.
                if (penultimatePostVersion != null)
                {
                    penultimatePostVersion.KeyOwnerUserPost = new [] { ownerId, post.UserId, post.Id };
                    penultimatePostVersion.KeyOwnerUserPostVersion = null;
                }

                // Depending on the result, either update the last version, or replace it with the penultimate.
                var lastVersionUpdateResult = penultimatePostVersion == null
                    ? await this.table
                        .Get(new[] { ownerId, post.UserId, post.Id })
                        .Update(r => this.db.R.Branch(!specificVersion
                                ? (object)r.Ne(null) 
                                : (object)r.Ne(null).And(r.G("version").Eq(post.Version.Id))
                            , deletedAt, null))
                        .RunResultAsync(c, null, cancellationToken)
                    : await this.table
                        .Get(new[] { ownerId, post.UserId, post.Id })
                        .Replace(r => this.db.R.Branch(r.Ne(null).And(r.G("version").Eq(post.Version.Id)), penultimatePostVersion, r))
                        .RunResultAsync(c, null, cancellationToken);

                lastVersionUpdateResult.AssertNoErrors();

                // Depending of whether a version id was specified, update one or more versions.
                var versionUpdateResult = await (!specificVersion
                    ? this.tableVersions.Between(
                            new object[] { ownerId, post.UserId, post.Id, this.db.R.Minval() },
                            new object[] { ownerId, post.UserId, post.Id, this.db.R.Maxval() })
                        .Update(deletedAt)
                    : this.tableVersions.Get(new[] { ownerId, post.UserId, post.Id, this.modelHelpers.GetShortVersionId(post.Version.Id) })
                        .Update(deletedAt))
                    .RunResultAsync(c, null, cancellationToken);

                versionUpdateResult.AssertNoErrors();
            }, cancellationToken);
        }
Ejemplo n.º 11
0
 public Task DeleteAsync(string ownerId, TentPost post, CancellationToken cancellationToken = new CancellationToken())
 {
     return this.DeleteAsync(ownerId, post, false, cancellationToken);
 }
Ejemplo n.º 12
0
 public Task<string> CreateBewitForPostAsync(User user, TentPost post, CancellationToken cancellationToken = default(CancellationToken))
 {
     return this.CreateBewitForPostAsync(user, post, this.configuration.DefaultBewitExpiration, cancellationToken);
 }
Ejemplo n.º 13
0
 public ITentFeedRequest AddPostBoundary(TentPost boundaryPost, TentFeedRequestBoundaryType boundaryType)
 {
     Ensure.Argument.IsNotNull(boundaryPost, nameof(boundaryPost));
     var requestPost = this.requestPostFactory.FromPost(boundaryPost);
     return this.AddPostBoundary(requestPost, boundaryType);
 }
Ejemplo n.º 14
0
 public ITentHawkSignature FromCredentials(TentPost<TentContentCredentials> credentials)
 {
     return new TentHawkSignature(this.cryptoHelpers, this.textHelpers, this.uriHelpers)
     {
         Id = credentials.Id,
         Key = Encoding.UTF8.GetBytes(credentials.Content.HawkKey),
         Type = HawkMacType.Header
     };
 }