Skip to content

Commit

Permalink
- Add Authentication
Browse files Browse the repository at this point in the history
- Use sqlite
  • Loading branch information
Nicolas Choquet committed Mar 17, 2024
1 parent 6c6d977 commit d2cff07
Show file tree
Hide file tree
Showing 37 changed files with 1,309 additions and 211 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea/
.dist/
.dist/

file-system-service-oauth.sqlite
169 changes: 169 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ Click on `file-system-service.swagger.yml`
##### Get Last binary
Click on `file-system-service-windows-{version}-windows-amd64.zip`

#### Generate Signature token
```shell
file-system-service --generate-signature
```

## API Reference

#### Check Validity
Expand All @@ -63,6 +68,79 @@ GET /check-validity
}
```

#### Authentification

##### Get first token

```http request
POST /auth/get-token
Accept: application/json
Content-Type: application/json
Signature-Token: {generated-signature}
```
###### Response 200

```json
{
"access_token": "string",
"refresh_token": "string",
"expires_in": "int",
"created_at": "int"
}
```
###### Response 400

```json
{
"code": 400,
"message": "string"
}
```
###### Response 500

```json
{
"code": 500,
"message": "string"
}
```

##### Refresh token

```http request
PUT /auth/get-token
Accept: application/json
Content-Type: application/json
Signature-Token: {generated-signature}
Refresh-Token: {getted-refresh-token}
```
###### Response 200

```json
{
"access_token": "string",
"refresh_token": "string",
"expires_in": "int",
"created_at": "int"
}
```
###### Response 400

```json
{
"code": 400,
"message": "string"
}
```
###### Response 500

```json
{
"code": 500,
"message": "string"
}
```

#### Get directory list content items

```http request
Expand Down Expand Up @@ -109,6 +187,15 @@ GET /file-system/${path...}
}
```

##### Response 403

```json
{
"code": 403,
"message": "an error message"
}
```

#### Create a directory

```http request
Expand Down Expand Up @@ -149,6 +236,15 @@ Content-Type: application/json
}
```

##### Response 403

```json
{
"code": 403,
"message": "an error message"
}
```

#### Rename directory

```http request
Expand Down Expand Up @@ -195,6 +291,15 @@ Content-Type: application/json
}
```

##### Response 403

```json
{
"code": 403,
"message": "an error message"
}
```

#### Delete directory

```http request
Expand Down Expand Up @@ -233,6 +338,15 @@ Accept: application/json
}
```

##### Response 403

```json
{
"code": 403,
"message": "an error message"
}
```

#### Create file with content

```http request
Expand Down Expand Up @@ -282,6 +396,15 @@ Ceci est un test
| :-------- | :------- | :-------------------------------- | :------------ |
| `path` | `string` | **Optional**. The path of the directory you would like open | / |

##### Response 403

```json
{
"code": 403,
"message": "an error message"
}
```

#### Create file without content

```http request
Expand Down Expand Up @@ -320,6 +443,15 @@ Content-Type: application/json
| :-------- | :------- | :-------------------------------- | :------------ |
| `path` | `string` | **Optional**. The path of the directory you would like open | / |

##### Response 403

```json
{
"code": 403,
"message": "an error message"
}
```

#### Get file content

```http request
Expand Down Expand Up @@ -357,6 +489,15 @@ Accept: application/json
}
```

##### Response 403

```json
{
"code": 403,
"message": "an error message"
}
```

#### Rename selected file

```http request
Expand Down Expand Up @@ -405,6 +546,15 @@ Content-Type: application/json
}
```

##### Response 403

```json
{
"code": 403,
"message": "an error message"
}
```

#### Update selected file content

```http request
Expand Down Expand Up @@ -442,6 +592,15 @@ the fichier
}
```

##### Response 403

```json
{
"code": 403,
"message": "an error message"
}
```

#### Delete selected file

```http request
Expand Down Expand Up @@ -474,3 +633,13 @@ Accept: application/json
"message": "an error message"
}
```


##### Response 403

```json
{
"code": 403,
"message": "an error message"
}
```
17 changes: 17 additions & 0 deletions actions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package actions

import (
"filesystem_service/auth"
"filesystem_service/flags"
)

func Exec() bool {
fl := flags.GetFlags()

if fl.IsGenerateSignature() {
auth.GenerateSignatureTokenAction()
return true
}

return false
}
23 changes: 23 additions & 0 deletions arrays/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package arrays

func Map[T, U any](ts []T, f func(T) U) []U {
us := make([]U, len(ts))
for i := range ts {
us[i] = f(ts[i])
}
return us
}

func Filter[T any](ts []T, f func(T) bool) []T {
us := []T{}
for i := range ts {
if pass := f(ts[i]); pass {
us = append(us, ts[i])
}
}
return us
}

func Generate[T any](length int) []T {
return make([]T, length)
}
50 changes: 50 additions & 0 deletions auth/checkToken.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package auth

import (
"filesystem_service/customHttp"
"filesystem_service/data"
"fmt"
"net/http"
"strings"
)

func CheckToken(request *http.Request) (bool, error) {
signature := request.Header.Get("Signature-Token")
token := strings.Replace(request.Header.Get("Authorization"), "Bearer ", "", 1)

if token == "" || signature == "" {
return false, fmt.Errorf("you are not authorized")
}

ip, err := customHttp.GetUserIp(request)
if err != nil {
return false, err
}

db, err := data.InitDatabase()
defer db.Close()
if err != nil {
return false, err
}

results, err := db.Query(fmt.Sprintf(
"SELECT * FROM tokens WHERE IP=\"%v\" AND active=TRUE AND type=\"classic\" AND token=\"%s\" AND signature=\"%s\"",
ip, token, signature,
))
if err != nil {
return false, err
}

tokens, err := data.ReadRows[Token](results, func(t *Token) error {
return results.Scan(&t.Id, &t.Ip, &t.Token, &t.Signature, &t.Type, &t.Active, &t.CreatedAt)
})
if err != nil {
return false, err
}

if len(tokens) == 0 {
return false, fmt.Errorf("invalid access token")
}

return true, nil
}
Loading

0 comments on commit d2cff07

Please sign in to comment.