Skip to content

Commit

Permalink
fix: improve code specification (casdoor#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
sh1luo authored and hsluoyz committed Aug 14, 2021
1 parent a271ef0 commit 8c66ef6
Show file tree
Hide file tree
Showing 36 changed files with 209 additions and 164 deletions.
File renamed without changes.
175 changes: 80 additions & 95 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,146 +1,131 @@
Casdoor
====

[![Go Report Card](https://goreportcard.com/badge/github.com/casbin/casdoor)](https://goreportcard.com/report/github.com/casbin/casdoor) <img src="https://img.shields.io/github/license/casbin/casdoor?style=flat-square" alt="license"> [![GitHub issues](https://img.shields.io/github/issues/casbin/casdoor?style=flat-square)](https://github.com/casbin/casdoor/issues) [![GitHub stars](https://img.shields.io/github/stars/casbin/casdoor?style=flat-square)](https://github.com/casbin/casdoor/stargazers) [![GitHub forks](https://img.shields.io/github/forks/casbin/casdoor?style=flat-square)](https://github.com/casbin/casdoor/network)

Casdoor is a UI-first centralized authentication / Single-Sign-On (SSO) platform based on OAuth 2.0 / OIDC.

## Online demo

### Casdoor
Deployed site: https://door.casbin.com/

## Quick Start

Run your own casdoor program in a few minutes:smiley:

### Download

There are two methods, get code via go subcommand `get`:

```shell
go get github.com/casbin/casdoor
```

or `git`:

Casdoor is the authentication server. It serves both the web UI and the login requests from the application users.
```bash
git clone https://github.com/casbin/casdoor
```

- Deployed site: https://door.casbin.com/
- Source code: https://github.com/casbin/casdoor (this repo)
Finally, change directory:

Global admin login:
```bash
cd casdoor/
```

- Username: `admin`
- Password: `123`
We provide two start up methods for all kinds of users.

### Web application
### Manual

Casbin-OA is one of our applications that use Casdoor as authentication.
#### Simple configuration

- Deployed site: https://oa.casbin.com/
- Source code: https://github.com/casbin/casbin-oa
Edit `conf/app.conf`, modify `dataSourceName` to correct database info, which follows this format:

## Architecture
```bash
username:password@tcp(database_ip:database_port)/
```

Casdoor contains 2 parts:
#### Run

Name | Description | Language | Source code
----|------|----|----
Frontend | Web frontend UI for Casdoor | Javascript + React | https://github.com/casbin/casdoor/tree/master/web
Backend | RESTful API backend for Casdoor | Golang + Beego + MySQL | https://github.com/casbin/casdoor
Casdoor provides two run modes, the difference is binary size and user prompt.

## Installation
##### Dev Mode

- Get code via `go get`:
Edit `conf/app.conf`, set `runmode=dev`. Firstly build front-end files:

```shell
go get github.com/casbin/casdoor
```
```bash
cd web/ && npm install && npm run start
```

or `git clone`:
Then build back-end binary file, change directory to root(Relative to casdoor):

```shell
git clone https://github.com/casbin/casdoor
```
```bash
go run main.go
```

## Run through Docker
- Install Docker and Docker-compose,you see [docker](https://docs.docker.com/get-docker/) and [docker-compose](https://docs.docker.com/compose/install/)
- vi casdoor/conf/app.conf
- Modify dataSourceName = root:123@tcp(localhost:3306)/ to dataSourceName = root:123@tcp(db:3306)/
- Execute the following command
```shell
docker-compose up
```
- Open browser:
That's it! Try to visit http://127.0.0.1:7001/. :small_airplane:

http://localhost:8000/
##### Prod Mode

## Run (Dev Environment)
Edit `conf/app.conf`, set `runmode=prod`. Firstly build front-end files:

- Run backend (in port 8000):
```bash
cd web/ && npm install && npm run build
```

```shell
go run main.go
```
Then build back-end binary file, change directory to root(Relative to casdoor):

- Run frontend (in the same machine's port 7001):
```bash
go build main.go && sudo ./main
```

```shell
cd web
## npm
npm install
npm run start
## yarn
yarn install
yarn run start
```
> Notice, you should visit back-end port, default 8000. Now try to visit http://127.0.0.1:8000/
- Open browser:
### Docker

http://localhost:7001/
This method requires [docker](https://docs.docker.com/get-docker/) and [docker-compose](https://docs.docker.com/compose/install/) to be installed first.

## Run (Production Environment)
#### Simple configuration

- build static pages:
Edit `conf/app.conf`, modify `dataSourceName` to the fixed content:

```
cd web
## npm
npm run build
## yarn
yarn run build
## back to casdoor directory
cd ..
```
```bash
dataSourceName = root:123@tcp(db:3306)/
```

- build and run go code:
> If you need to modify `conf/app.conf`, you need to re-run `docker-compose up`.
```
go build
./casdoor
```
#### Run

Now, Casdoor is running on port 8000. You can access Casdoor pages directly in your browser, or you can setup a reverse proxy to hold your domain name, SSL, etc.
Just execute:

## Config
```bash
docker-compose up
```

- Setup database (MySQL):
That's

Casdoor will store its users, nodes and topics informations in a MySQL database named: `casdoor`, will create it if not existed. The DB connection string can be specified at: https://github.com/casbin/casdoor/blob/master/conf/app.conf
## Detailed documentation

```ini
db = mysql
dataSourceName = root:123@tcp(localhost:3306)/
dbName = casdoor
```
We also provide a complete [document](https://casdoor.org/) as a reference.

- Setup database (Postgres):
## Other examples

Since we must choose a database when opening Postgres with xorm, you should prepare a database manually before running Casdoor. Let's assume that you have already prepared a database called `casdoor`, then you should specify `app.conf` like this:
These all use casdoor as a centralized authentication platform.

``` ini
db = postgres
dataSourceName = "user=postgres password=xxx sslmode=disable dbname="
dbName = casdoor
```
- [Casnode](https://github.com/casbin/casnode): Next-generation forum software based on React + Golang.
- [Casbin-OA](https://github.com/casbin/casbin-oa): A full-featured OA(Office Assistant) system.
- ......

**Please notice:** You can add Postgres parameters in `dataSourceName`, but please make sure that `dataSourceName` ends with `dbname=`. Or database adapter may crash when you launch Casdoor.
## Contribute

Casdoor uses XORM to connect to DB, so all DBs supported by XORM can also be used.
For casdoor, if you have any questions, you can give Issues, and you can also directly Pull Requests(but we recommend give issues first to communicate with the community).

- Github corner
In addition to contributing codes to casdoor, uou can also contribute to the [internationalization of casdoor](https://crowdin.com/project/casdoor-web) by translating for casdoor in the language you are good at.

We added a Github icon in the upper right corner, linking to your Github repository address.
You could set `ShowGithubCorner` to hidden it.
Both are welcome! :kissing_smiling_eyes:

Configuration (`web/src/commo/Conf.js`):
## License

```javascript
export const ShowGithubCorner = true
[Apache-2.0](https://github.com/casbin/casdoor/blob/master/LICENSE)

export const GithubRepo = "https://github.com/casbin/casdoor" //your github repository
```
5 changes: 5 additions & 0 deletions controllers/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type HumanCheck struct {
CaptchaImage interface{} `json:"captchaImage"`
}

// Signup
// @Title Signup
// @Description sign up a new user
// @Param username formData string true "The username to sign up"
Expand Down Expand Up @@ -178,6 +179,7 @@ func (c *ApiController) Signup() {
c.ServeJSON()
}

// Logout
// @Title Logout
// @Description logout the current user
// @Success 200 {object} controllers.Response The Response object
Expand All @@ -197,6 +199,7 @@ func (c *ApiController) Logout() {
c.ServeJSON()
}

// GetAccount
// @Title GetAccount
// @Description get the details of the current account
// @Success 200 {object} controllers.Response The Response object
Expand Down Expand Up @@ -224,6 +227,7 @@ func (c *ApiController) GetAccount() {
c.ServeJSON()
}

// UploadAvatar
// @Title UploadAvatar
// @Description upload avatar
// @Param avatarfile formData string true "The base64 encode of avatarfile"
Expand Down Expand Up @@ -272,6 +276,7 @@ func (c *ApiController) UploadAvatar() {
c.ServeJSON()
}

// GetHumanCheck ...
func (c *ApiController) GetHumanCheck() {
c.Data["json"] = HumanCheck{Type: "none"}

Expand Down
6 changes: 6 additions & 0 deletions controllers/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/casbin/casdoor/object"
)

// GetApplications
// @Title GetApplications
// @Description get all applications
// @Param owner query string true "The owner of applications."
Expand All @@ -32,6 +33,7 @@ func (c *ApiController) GetApplications() {
c.ServeJSON()
}

// GetApplication
// @Title GetApplication
// @Description get the detail of an application
// @Param id query string true "The id of the application."
Expand All @@ -44,6 +46,7 @@ func (c *ApiController) GetApplication() {
c.ServeJSON()
}

// GetUserApplication
// @Title GetUserApplication
// @Description get the detail of the user's application
// @Param id query string true "The id of the user"
Expand All @@ -61,6 +64,7 @@ func (c *ApiController) GetUserApplication() {
c.ServeJSON()
}

// UpdateApplication
// @Title UpdateApplication
// @Description update an application
// @Param id query string true "The id of the application"
Expand All @@ -80,6 +84,7 @@ func (c *ApiController) UpdateApplication() {
c.ServeJSON()
}

// AddApplication
// @Title AddApplication
// @Description add an application
// @Param body body object.Application true "The details of the application"
Expand All @@ -96,6 +101,7 @@ func (c *ApiController) AddApplication() {
c.ServeJSON()
}

// DeleteApplication
// @Title DeleteApplication
// @Description delete an application
// @Param body body object.Application true "The details of the application"
Expand Down
14 changes: 7 additions & 7 deletions controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ import (
func codeToResponse(code *object.Code) *Response {
if code.Code == "" {
return &Response{Status: "error", Msg: code.Message, Data: code.Code}
} else {
return &Response{Status: "ok", Msg: "", Data: code.Code}
}

return &Response{Status: "ok", Msg: "", Data: code.Code}
}

func (c *ApiController) HandleLoggedIn(application *object.Application, user *object.User, form *RequestForm) *Response {
// HandleLoggedIn ...
func (c *ApiController) HandleLoggedIn(application *object.Application, user *object.User, form *RequestForm) (resp *Response) {
userId := user.GetId()
resp := &Response{}
if form.Type == ResponseTypeLogin {
c.SetSessionUsername(userId)
util.LogInfo(c.Ctx, "API: [%s] signed in", userId)
Expand Down Expand Up @@ -72,6 +72,7 @@ func (c *ApiController) HandleLoggedIn(application *object.Application, user *ob
return resp
}

// GetApplicationLogin ...
// @Title GetApplicationLogin
// @Description get application login
// @Param clientId query string true "client id"
Expand Down Expand Up @@ -108,6 +109,7 @@ func setHttpClient(idProvider idp.IdProvider, providerType string) {
}
}

// Login ...
// @Title Login
// @Description login
// @Param oAuthParams query string true "oAuth parameters"
Expand Down Expand Up @@ -182,13 +184,11 @@ func (c *ApiController) Login() {
c.ResponseError("wrong email!")
}
object.DisableVerificationCode(form.Email)
break
case "phone":
if user.Phone != form.Email {
c.ResponseError("wrong phone!")
}
object.DisableVerificationCode(form.Email)
break
}
} else {
password := form.Password
Expand Down Expand Up @@ -282,7 +282,7 @@ func (c *ApiController) Login() {
record.Organization = application.Organization
record.Username = user.Name

object.AddRecord(record)
object.AddRecord(record)
} else {
// Sign up via OAuth
if !application.EnableSignUp {
Expand Down
Loading

0 comments on commit 8c66ef6

Please sign in to comment.