ssojwt

package module
v0.0.0-...-fe7320a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2022 License: MIT Imports: 11 Imported by: 0

README

golang-sso-ui-jwt

Golang Library to use SSO UI as JWT login

Installation

go get github.com/ristekoss/golang-uisso-jwt

How to Use

see example folder

login handler

// set config
config := ssojwt.MakeSSOConfig(time.Hour*168, time.Hour*720, "super secret access", "super secret refresh", "http://localhost:8080/login", "http://localhost:8080/")

// create middleware
authMiddleware := ssojwt.LoginCreator(config, nil)

// use handler func
http.HandleFunc("/login", authMiddleware)

on the frontend

const loginHandler = async () => {
  const data = await popUpLogin();
  const e = document.getElementById("res");
  e.innerHTML = JSON.stringify(data, null, 2);
  localStorage.setItem("ssoui", JSON.stringify(data));
};

const popUpLogin = () => {
  const SSOWindow = window.open(
    new URL(
      // change service to what your backend url use and url encode it
      "https://sso.ui.ac.id/cas2/login?service=http%3A%2F%2Flocalhost%3A8080%2Flogin"
    ).toString(),
    "SSO UI Login",
    "left=50, top=50, width=480, height=480"
  );

  return new Promise(function (resolve, reject) {
    window.addEventListener(
      "message",
      (e) => {
        if (SSOWindow) {
          SSOWindow.close();
        }
        const data = e.data;
        resolve(data);
      },
      { once: true }
    );
  });
};

authenticated middleware

// set config
config := ssojwt.MakeSSOConfig(time.Hour*168, time.Hour*720, "super secret access", "super secret refresh", "http://localhost:8080/login", "http://localhost:8080/")

// create middleware
middle := ssojwt.MakeAccessTokenMiddleware(config, "user")

// use handler func
auth := middle(handler)

http.Handle("/check", auth)

authenticated middleware

// set config
config := ssojwt.MakeSSOConfig(time.Hour*168, time.Hour*720, "super secret access", "super secret refresh", "http://localhost:8080/login", "http://localhost:8080/")

// create middleware
middle := ssojwt.MakeRefreshTokenMiddleware(config)

// use handler func
http.Handle("/refresh", middle)
To-dos
  • create fiber handler
  • create better readme.md

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateAccessToken

func CreateAccessToken(config SSOConfig, ssoResponse ServiceResponse) (token string, err error)

func CreateRefreshToken

func CreateRefreshToken(config SSOConfig, ssoResponse ServiceResponse) (token string, err error)

func LoginCreator

func LoginCreator(config SSOConfig, errorLogger *log.Logger) func(w http.ResponseWriter, r *http.Request)

func MakeAccessTokenMiddleware

func MakeAccessTokenMiddleware(config SSOConfig, key string) func(nextHandler http.Handler) http.Handler

func MakeRefreshTokenMiddleware

func MakeRefreshTokenMiddleware(config SSOConfig) http.Handler

func ReadOrgcode

func ReadOrgcode() (data map[string]Jurusan)

func TemplateRenderHandler

func TemplateRenderHandler(data interface{}, config SSOConfig, w http.ResponseWriter) (err error)

func ValidatTicket

func ValidatTicket(config SSOConfig, ticket string) (bodyBytes []byte, err error)

Types

type Attributes

type Attributes struct {
	XMLName    xml.Name `xml:"attributes" json:"-"`
	Ldap_cn    string   `xml:"ldap_cn" xml:"ldap_cn"`
	Kd_org     string   `xml:"kd_org" json:"kd_org"`
	Peran_user string   `xml:"peran_user" json:"peran_user"`
	Nama       string   `xml:"nama" json:"nama"`
	Npm        string   `xml:"npm" json:"npm"`
	Jurusan    Jurusan  `json:"jurusan"`
}

type AuthenticationSuccess

type AuthenticationSuccess struct {
	XMLName    xml.Name   `xml:"authenticationSuccess" json:"-"`
	User       string     `xml:"user" json:"user"`
	Attributes Attributes `xml:"attributes" json:"attributes"`
}

type DataRender

type DataRender struct {
	LoginResponse string
	OriginUrl     string
}

func MakeTemplate

func MakeTemplate(config SSOConfig, res interface{}) (tmpl *template.Template, dataRender DataRender, err error)

type Jurusan

type Jurusan struct {
	Faculty      string `json:"faculty"`
	ShortFaculty string `json:"shortFaculty"`
	Major        string `json:"major"`
	Program      string `json:"program"`
}

type LoginResponse

type LoginResponse struct {
	AccessToken  string   `json:"access_token"`
	RefreshToken string   `json:"refresh_token"`
	Nama         string   `json:"nama,omitempty"`
	Npm          string   `json:"npm,omitempty"`
	Fakultas     *Jurusan `json:"fakultas,omitempty"`
}

func LoginRequestHandler

func LoginRequestHandler(ticket string, config SSOConfig) (res LoginResponse, err error)

func MakeLoginResponse

func MakeLoginResponse(config SSOConfig, model ServiceResponse) (res LoginResponse, err error)

type SSOConfig

type SSOConfig struct {
	AccessTokenExpireTime  time.Duration
	RefreshTokenExpireTime time.Duration
	AccessTokenSecretKey   string
	RefreshTokenSecretKey  string
	ServiceUrl             string
	OriginUrl              string
	CasURL                 string
}

func MakeSSOConfig

func MakeSSOConfig(accessTokenExpireTime, refreshTokenExpireTime time.Duration, accessTokenSecretKey, refreshTokenSecretKey, serviceUrl, originUrl string) SSOConfig

type SSOJwtClaim

type SSOJwtClaim struct {
	Nama    string  `json:"nama"`
	User    string  `json:"user"`
	Npm     string  `json:"npm"`
	Jurusan Jurusan `json:"jurusan"`
	jwt.RegisteredClaims
}

type ServiceResponse

type ServiceResponse struct {
	XMLName               xml.Name              `xml:"serviceResponse" json:"-"`
	AuthenticationSuccess AuthenticationSuccess `xml:"authenticationSuccess"`
}

func Unmarshal

func Unmarshal(bodyBytes []byte) (model ServiceResponse, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL