casdoor-go-sdk

module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2021 License: Apache-2.0

README

casdoor-go-sdk

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. Init Config

Initialization requires 5 parameters, which are all string type:

Name (in order) Must Description
endpoint Yes Casdoor Server Url, such as http://localhost:8000
clientId Yes Application.client_id
clientSecret Yes Application.client_secret
jwtSecret Yes Same as Casdoor JWT secret.
organizationName Yes Application.organization
func InitConfig(endpoint string, clientId string, clientSecret string, jwtSecret string, organizationName string)

Step2. Get token and parse

After casdoor verification passed, it will be redirected to your application with code and state, like http://forum.casbin.org?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 := auth.GetOAuthToken(code, state)
if err != nil {
	panic(err)
}

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

claims.AccessToken = token.AccessToken

Step3. 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)

Step4. 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(auth.User)/AddUser(auth.User)/DeleteUser(auth.User), write user to database.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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