Skip to content

mahmoudnaif/cattoapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API Management

CattoAPI

CattoAPI is a robust and versatile API solution designed to facilitate the management of posts, comments, likes, and email services in your applications. Built with .NET 8.0, it offers comprehensive features for user authentication, administrative actions, and seamless integration with various services. Whether you're building a social media platform, a blogging site, or any application requiring user interactions, CattoAPI provides the necessary endpoints and tools to get started quickly and efficiently.

Table of Contents

Overview

CattoAPI is built using ASP.NET Core and utilizes various libraries and frameworks to provide robust functionality for managing users, posts, comments, likes, and email services. It includes features for token management, password reset via email, and role-based access control.

Features

  • Multi-language Support: Available in various languages including English, Spanish, French, and more.
  • Comprehensive API Endpoints: Manage posts, comments, likes, and email services.
  • Blacklist Token Management: Secure your API with token blacklisting.
  • Email Services: Send and manage email notifications.
  • Advanced Interaction Handling: Features for managing user interactions and post engagements.

Technologies Used

  • .NET 8.0: Framework for building and running the API.
  • C#: Programming language used for development.
  • SQL Server: Database management for data persistence.
  • GitHub Actions: CI/CD workflows for automated testing and deployment.

Installation

To use CattoAPI, follow these steps:

  1. Clone the repository:

    git clone https://github.com/your-username/cattoapi.git
    cd cattoapi
  2. Configure the application:

    • Set up your database connection in appsettings.json.
    • Configure any environment-specific settings in appsettings.{Environment}.json.
  3. Build and run the application:

    dotnet build
    dotnet run

    The API will be accessible at https://localhost:5001 (or http://localhost:5000).

  4. Explore the API endpoints described below.

API Endpoints

To use the API, make requests to the endpoints provided below. Here are some example requests:

Accounts

  • Get All Accounts:

    GET /api/Accounts
  • Get Account by ID:

    GET /api/Accounts/id/{strId}
  • Get Account by Email:

    GET /api/Accounts/email/{email}
  • Search Accounts:

    GET /api/Accounts/search

Admin

  • Change Password:

    PUT /api/Admin/Changepassword
  • Change Email:

    PUT /api/Admin/changeEmail
  • Change Username:

    PUT /api/Admin/ChangeUserName
  • Change Role:

    PUT /api/Admin/ChangeRole
  • Verify Account:

    PUT /api/Admin/VerifyAccount
  • Remove Profile Picture:

    DELETE /api/Admin/RemovePFP
  • Delete Account:

    DELETE /api/Admin/DeleteAccount

Comment

  • Get User Comments:

    GET /api/User/Comments
  • Post a Comment:

    POST /api/User/Comments
  • Edit a Comment:

    PUT /api/User/Comments
  • Delete a Comment:

    DELETE /api/User/Comments
  • Get Post Comments:

    GET /api/Posts/Comments

Email Services

  • Send Verification Email:

    POST /api/EmailServices/SendVerficationEmail
  • Verify Email:

    PUT /api/EmailServices/VerifyEmail
  • Send Change Password Email:

    POST /api/EmailServices/SendChangePasswordEmail
  • Change Password:

    PUT /api/EmailServices/ChangePassword

Likes

  • Get Post Likes:

    GET /api/Likes/post
  • Get User Likes:

    GET /api/Likes/user
  • Post a Like:

    POST /api/Likes
  • Delete a Like:

    DELETE /api/Likes

Login

  • User Login:

    POST /api/login

Posts

  • Get User Posts:

    GET /api/User/Posts/GetPosts
  • Post a New Post:

    POST /api/User/Posts/PostaPost
  • Edit a Post:

    POST /api/User/Posts/EditaPost
  • Delete a Post:

    DELETE /api/User/Posts/DeletePost

Signup

  • User Signup:

    POST /api/Siqnup

User

  • Get User Data:

    GET /api/User/GetData
  • Change Password:

    PUT /api/User/ChangePassword
  • Change Profile Picture:

    PUT /api/User/ChangePFP
  • Get User by ID or Username:

    GET /api/User/{strIdOrUsername}
  • Get User Posts by ID or Username:

    GET /api/User/{strIdOrUsername}/posts

Schemas

AccountDTO

{
  "accountId": "integer($int64)",
  "email": "string",
  "userName": "string",
  "pfp": "string($byte)",
  "role": "string",
  "verified": "boolean"
}

AdminChangeModel

{
  "email": "string",
  "probertyChange": "string"
}

ChangePasswordModel

{
  "oldPassword": "string",
  "newPassword": "string"
}

EditCommentModel

{
  "commentId": "integer($int32)",
  "commentText": "string"
}

EditPostModel

{
  "postId": "integer($int32)",
  "data": "PostaPostModel"
}

PostCommentModel

{
  "postId": "integer($int32)",
  "commentText": "string"
}

PostaPostModel

{
  "postText": "string",
  "postPictrue": "string"
}

Siqninmodel

{
  "emailOrUserName": "string",
  "password": "string"
}

SiqnupModel

{
  "email": "string",
  "userName": "string",
  "password": "string",
  "repeatPassword": "string"
}

Security

CattoAPI implements various security measures:

  • Authorization: Endpoints are protected using JWT (JSON Web Token) authentication.
  • Token Blacklisting: Tokens are blacklisted in memory cache to prevent reuse after a certain time.
  • Password Management: Secure password storage and change processes are enforced.

CustomResponse Class

The CustomResponse<T> class standardizes the shape of API responses across the application. It includes fields for response code, message, and optional data.

namespace cattoapi.CustomResponse
{
    public class CustomResponse<T>
    {
        public CustomResponse()
        {
            responseCode = 0;
            responseMessage = string.Empty;
        }

        public CustomResponse(int responseCode, string responseMessage)
        {
            this.responseCode = responseCode;
            this.responseMessage = responseMessage;
        }

        public CustomResponse(int responseCode, string responseMessage, T data)
        {
            this.responseCode = responseCode;
            this.responseMessage = responseMessage;
            this.data = data;
        }

        public int responseCode { get; set; }
        public string responseMessage { get; set; }
        public T? data { get; set; }
    }
}

Blacklisting Tokens in Cache

Tokens are blacklisted in a cache after a certain time to prevent their reuse, ensuring that tokens issued before a password change are promptly invalidated.

Implementation Details The BlackListTokensRepo class manages token blacklisting using an in-memory cache (IMemoryCache). It provides methods to blacklist tokens based on the account ID, timestamp, and token type (e.g., login token or email verification token).

using cattoapi.Interfaces.BlackListTokens;
using Microsoft.Extensions.Caching.Memory;
using static cattoapi.utlities.Utlities; // Assuming this includes TokenType enum

namespace cattoapi.Repos.BlackListTokens
{
    public class BlackListTokensRepo : IBlackListTokensRepo
    {
        private readonly IMemoryCache _memoryCache;

        public BlackListTokensRepo(IMemoryCache memoryCache) {
            _memoryCache = memoryCache;
        }

        public Task BlacklistTokensAsync(int accountId, DateTime timestamp, TokenType tokenType)
        {
            int timeCached = 60; // default value
            switch (tokenType)
            {
                case TokenType.Login:
                    timeCached = 3 * 60; // 3 hours
                    break;

                case TokenType.EmailToken:
                    timeCached = 10; // 10 minutes
                    break;
            }

            _memoryCache.Set(accountId, timestamp, new MemoryCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(timeCached)
            });
            return Task.CompletedTask;
        }

        public Task<bool> IsTokenBlacklisted(int accountId, DateTime issuedAt)
        {
            if (_memoryCache.TryGetValue(accountId, out DateTime blacklistTime))
            {
                return Task.FromResult(issuedAt <= blacklistTime);
            }
            return Task.FromResult(false);
        }
    }
}

Dependencies

CattoAPI relies on several key dependencies:

  • ASP.NET Core: Web framework
  • Entity Framework Core: ORM for database operations
  • Microsoft.Extensions.Caching.Memory: Memory caching for token management
  • Newtonsoft.Json: JSON serialization
  • Microsoft.Identity.Client: Authentication library for JWT

Contact

For questions or inquiries, please contact us at [email protected].

Note

Note

Please note that the code in this repository will not work out-of-the-box because appsettings.json is in .gitignore. This file typically contains sensitive information such as passwords, JWT keys, connection strings, and other configuration details specific to your environment. Before running the application, ensure you have set up your appsettings.json file correctly with appropriate values for your environment.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published