A demo of authentication and authorization using jwt

Nogopy

Hi, this a demo of how to use jwt for authentication in microservices

Keep in mind that this is a demo of how to authenticate using jwt, we don't cover any issues such as sql injection, you can easily produce it to hack the server.

Project layout

.
├── cmd                    # Main applications.
├── config                 # Configuration file templates or default configs.
    ├── database           # Config for database.
    ├── environment        # Config for environment variables.
    ├── infrastructure     # Config for http server and worker jobs.
├── docs                   # Swagger API.
├── internal               # Private application and library code.
    ├── app
        ├── jwt-for-authentication
            ├── controllers        # REST controllers.
            ├── dto                # Data transfer object.
            ├── middlware          # Middleware of REST server.
            ├── models             # Model of RDBMS or NoSQL.
            ├── repositories       # Repositories layer.
            ├── routers            # Routing.
            ├── services           # Business services.
            ├── utils              # Utility.
├── script                         # Scripts to perform various build or test
└── README.md

Dependency

Nogopy uses a number of open source projects to work properly:

  • Gorm - The fantastic ORM library for Golang
  • Gin - Gin is a web framework written in Go
  • Logrus - Logrus is a structured logger for Go

Installation

Golang-template requires Golang v1.16 to run.

Install the dependencies and devDependencies and start the server.

export PATH=$PATH:$HOME/go/bin
go mod init github.com/nogopy/jwt-for-authentication
go mod tidy
go get github.com/codegangsta/gin

Run

Run a local mysql server if you don't have a remote one. Here I'm on Mac

alt text for screen readers

Run script to create a simple login database and user table

cd /usr/local/mysql/bin
./mysql -u root -p then enter password
create database login
CREATE TABLE `user` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(200) NOT NULL,
  `password` varchar(200) NOT NULL,
  `created_at` datetime DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `idx_username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Then use a tool to check if everything is ok, I recommend use Datagrip

alt text for screen readers

Clone .env.example to .env then input your config

cd into jwt-for-authentication and create your private key and public key

ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key
# Don't add passphrase
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub

Finally, run the script file and enjoy

./script/run.sh

Verify the deployment by navigating to your server address in your preferred browser.

127.0.0.1:8080

alt text for screen readers

------------------------------------------------------------

What is jwt and how it works https://jwt.io/introduction

Jwt flow

alt text for screen readers Source: https://www.freecodecamp.org/news/how-to-setup-jwt-authorization-and-authentication-in-spring/

Similar Resources

🔥 Golang Rest Api with basic JWT Authentication and Basic Crud Operations.

🔥 Golang Rest Api with basic JWT Authentication and Basic Crud Operations.

Oct 4, 2022

Authentication Plugin for implementing Form-Based, Basic, Local, LDAP, OpenID Connect, OAuth 2.0, SAML Authentication

Authentication Plugin for implementing Form-Based, Basic, Local, LDAP, OpenID Connect, OAuth 2.0, SAML Authentication

Authentication Plugin for implementing Form-Based, Basic, Local, LDAP, OpenID Connect, OAuth 2.0, SAML Authentication

Jan 8, 2023

A simple passwordless authentication middleware that uses only email as the authentication provider

A simple passwordless authentication middleware that uses only email as the authentication provider

email auth A simple passwordless authentication middleware that uses only email as the authentication provider. Motivation I wanted to restrict access

Jul 27, 2022

Barbar Service is a project for demo of simple distribute authentication Service.

Barbar Service is a project for demo of simple distribute authentication Service.

Barbar Service Barbar Service is a project for demo of simple distribute authentication Service. Project Structure Basicaly we reffer to Domain Driven

Aug 29, 2022

Vault Authentication Azure Auth Method Demo Application

Vault Authentication Azure Auth Method Demo Application

This application demonstrates how applications hosted on Azure VMs can use managed identity to authenticate with HashiCorp Vault using the Azure Auth Method.

Feb 10, 2022

telegram authorization in telegram without using a widget

TGAH - telegram Authorization Example of authorization in telegram without using a widget Installation go get -d github.com/tioffs/tgah@master Setti

Jun 6, 2022

🔑 Authz0 is an automated authorization test tool. Unauthorized access can be identified based on URL and Role.

🔑 Authz0 is an automated authorization test tool. Unauthorized access can be identified based on URL and Role.

Authz0 is an automated authorization test tool. Unauthorized access can be identified based on URL and Role. URLs and Roles are managed as YAML-based

Dec 20, 2022

Goauth: Pre-made OAuth/OpenIDConnect and general authorization hooks for webapp login

goauth Pre-made OAuth/OpenIDConnect and general authorization hooks for webapp login. Currently supports Google, Facebook and Microsoft "out of the bo

Jan 28, 2022

Golang Mongodb Jwt Auth Example Using Echo

Golang Mongodb Jwt Auth Example Using Echo

Golang Mongodb Jwt Auth Example Using Echo Golang Mongodb Rest Api Example Using Echo Prerequisites Golang 1.16.x Docker 19.03+ Docker Compose 1.25+ I

Nov 30, 2022
Authelia: an open-source authentication and authorization server providing two-factor authentication
Authelia: an open-source authentication and authorization server providing two-factor authentication

Authelia is an open-source authentication and authorization server providing two

Jan 5, 2022
Go-auth - An authorization project using mongoDB, JWT and Go
Go-auth - An authorization project using mongoDB, JWT and Go

Ssibrahimbas Go-Auth An authorization project using mongoDB, JWT and Go. API Typ

Mar 10, 2022
Go-gin-jwt - Secure web api using jwt token and caching mechanism

Project Description This project demonstrate how to create api and secure it wit

Jan 27, 2022
Backend Development Rest Api Project for book management system. Used Features like redis, jwt token,validation and authorization.

Golang-restapi-project Simple Rest Api Project with Authentication, Autherization,Validation and Connection with redis File Structure ├── cache │ ├──

May 25, 2022
Mini-framework for multiple authentication and authorization schemes
Mini-framework for multiple authentication and authorization schemes

Go authorization pattern This repository demonstrates an authorization pattern that allows multiple schemes. Demo To start the demo run the following

Dec 30, 2021
A simple authentication web application in Golang (using jwt)

Simple Authentication WebApp A simple authentication web app in Go (using JWT) Routes Path Method Data /api/v1/auth/register POST {"firstname":,"lastn

Feb 6, 2022
Example of a simple application which is powered by a third-party oAuth 2.0 server for it's authentication / authorization. Written in Golang.

go mod init github.com/bartmika/osin-thirdparty-example go get github.com/spf13/cobra go get github.com/openshift/osin go get github.com/openshift/osi

Jan 4, 2022
JWT wrapper library which makes it simple to use ECDSA based JWT signing

JWT JWT wrapper library which makes it simple to user ECDSA based JWT signing. Usage package main import ( "context" "github.com/infiniteloopcloud

Feb 10, 2022
Account-jwt-go - Simple JWT api with go, gorm, gin
Account-jwt-go - Simple JWT api with go, gorm, gin

Account JWT on Go Go, gorm, Gin web framework 를 활용하여 만든 간단한 JWT API 입니다. Dajngo의

Apr 14, 2022
Krakend-jwt-header-rewriter - Kraken Plugin - JWT Header Rewriter

Kraken Plugin - JWT Header Rewriter 1 Plugin Configuration Name Desciption Defau

Feb 15, 2022