GithubHelp home page GithubHelp logo

leo220yuyaodog / casdoor-go-sdk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from casdoor/casdoor-go-sdk

0.0 0.0 0.0 156 KB

Go client SDK for Casdoor

Home Page: https://github.com/casdoor/casdoor

License: Apache License 2.0

Go 100.00%

casdoor-go-sdk's Introduction

casdoor-go-sdk

semantic-release GitHub Workflow Status (branch) GitHub Release

Go Report Card license GitHub issues GitHub stars GitHub forks Casdoor

This is Casdoor's SDK for golang, which will allow you to easily connect your application to the Casdoor authentication system without having to implement it from scratch.

Casdoor SDK is very simple to use. We will show you the steps below.

Noted that this sdk has been applied to casnode, if you still don’t know how to use it after reading README.md, you can refer to it

Step1. Install and Import

First in your go project, just need to run:

go get github.com/casdoor/casdoor-go-sdk@latest

and import this when you need:

import "github.com/casdoor/casdoor-go-sdk/casdoorsdk"

Step2. Init

Initialization requires 6 parameters, which are all string type:

Name (in order) Must Description
endpoint Yes Casdoor server URL, such as http://localhost:8000
clientId Yes Application.clientId
clientSecret Yes Application.clientSecret
certificate Yes x509 certificate content of Application.cert
organizationName Yes Application.organization
applicationName Yes Application.applicationName

You can either init the sdk with global config

func InitConfig(endpoint string, clientId string, clientSecret string, certificate string, organizationName string, applicationName string)

// Then call sdk functions like
casdoorsdk.GetUsers()

or create a custom Client with unique config

client := casdoorsdk.NewClient(endpoint, clientId, clientSecret, certificate, organizationName, applicationName)
client.GetUsers()

Step3. Get token and parse

After casdoor verification passed, it will be redirected to your application with code and state, like https://forum.casbin.com?code=xxx&state=yyyy.

Your web application can get the code,state and call GetOAuthToken(code, state), then parse out jwt token.

The general process is as follows:

token, err := casdoorsdk.GetOAuthToken(code, state)
if err != nil {
	panic(err)
}

claims, err := casdoorsdk.ParseJwtToken(token.AccessToken)
if err != nil {
	panic(err)
}

claims.AccessToken = token.AccessToken

Step4. Set Session in your app

auth.Claims contains the basic information about the user provided by casdoor, you can use it as a keyword to set the session in your application, like this:

data, _ := json.Marshal(claims)
c.setSession("user", data)

Step5. Interact with the users

Casdoor-go-sdk support basic user operations, like:

  • GetUser(name string), get one user by user name.
  • GetUsers(), get all users.
  • UpdateUser(casdoorsdk.User)/AddUser(casdoorsdk.User)/DeleteUser(casdoorsdk.User), write user to database.

casdoor-go-sdk's People

Contributors

hsluoyz avatar nomeguy avatar qianxi0410 avatar thaihuynhxyz avatar notdu avatar hound672 avatar leoil avatar wintbiit avatar ebreak avatar towerhe avatar sh1luo avatar qimeila avatar sealingp avatar halozhy avatar selflocking avatar steve0x2a avatar thanhtinhpas1 avatar resulte avatar nekotoxin avatar dirname avatar follisher avatar abingcbc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.