https://github.com/casdoor/casdoor-go-sdk
Go client SDK for Casdoor
https://github.com/casdoor/casdoor-go-sdk
auth authentication authn casdoor go golang jwt oauth oidc sdk
Last synced: about 2 months ago
JSON representation
Go client SDK for Casdoor
- Host: GitHub
- URL: https://github.com/casdoor/casdoor-go-sdk
- Owner: casdoor
- License: apache-2.0
- Created: 2021-05-29T15:53:56.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2025-03-08T11:30:38.000Z (3 months ago)
- Last Synced: 2025-04-01T18:20:31.927Z (about 2 months ago)
- Topics: auth, authentication, authn, casdoor, go, golang, jwt, oauth, oidc, sdk
- Language: Go
- Homepage: https://github.com/casdoor/casdoor
- Size: 262 KB
- Stars: 98
- Watchers: 2
- Forks: 76
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# casdoor-go-sdk
This is Casdoor's SDK for Go, which will allow you to easily connect your application to the Casdoor authentication system without having to implement it from scratch.
Casdoor Go SDK is very simple to use. We will show you the steps below.
## Step 1. Install and Import
First in your go project, just need to run:
```bash
go get github.com/casdoor/casdoor-go-sdk@latest
```and import this when you need:
```go
import "github.com/casdoor/casdoor-go-sdk/casdoorsdk"
```## Step 2. 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 initialize the SDK with global config
```go
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
```go
client := casdoorsdk.NewClient(endpoint, clientId, clientSecret, certificate, organizationName, applicationName)
client.GetUsers()
```## Step 3. 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:
```go
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
```## Step 4. 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:
```go
data, _ := json.Marshal(claims)
c.setSession("user", data)
```## Step 5. 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.