Skip to content

Commit

Permalink
- Add Oauth authentication
Browse files Browse the repository at this point in the history
- Add internal Swagger UI
- Add cors
  • Loading branch information
Nicolas Choquet committed Mar 22, 2024
1 parent 7320ff7 commit f04423c
Show file tree
Hide file tree
Showing 63 changed files with 2,428 additions and 739 deletions.
Empty file modified .github/workflows/build.yml
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
5 changes: 5 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ Click on `file-system-service-windows-{version}-windows-amd64.zip`
file-system-service --generate-signature
```

## Swagger
- [Fichiers de définitions json](./swagger/swagger.json)
- [Fichiers de définitions yaml](./swagger/swagger.yaml)
- [Swagger UI accessible ici](http://localhost:3000/swagger)

## API Reference

#### Check Validity
Expand Down
22 changes: 19 additions & 3 deletions actions/main.go
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package actions

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

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

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

if fl.IsUpdateCredentials() {
credentials.UpdateCredentials(fl.GetClientId(), fl.GetRole())
return true
}

if fl.IsShowUserRole() {
roles.GetUserRole(fl.GetClientId())
return true
}

if fl.IsGenerateCredentials() {
credentials.GenerateCredentials(fl.GetRole())
return true
}

Expand Down
16 changes: 16 additions & 0 deletions arrays/main.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ func Filter[T any](ts []T, f func(T) bool) []T {
func Generate[T any](length int) []T {
return make([]T, length)
}

func Keys[T any](arr []T) (keys []int) {
for i, _ := range arr {
keys = append(keys, i)
}
return
}

func IsIn[T comparable](v T, a []T) bool {
for _, val := range a {
if v == val {
return true
}
}
return false
}
65 changes: 0 additions & 65 deletions auth/checkToken.go

This file was deleted.

19 changes: 19 additions & 0 deletions auth/credentials/createNewToken.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package credentials

import (
"filesystem_service/arrays"
"filesystem_service/integer"
"strings"
)

func CreateNewToken(availableCharacters string, charNumber int) string {
return strings.Join(
arrays.Map[string, string](
arrays.Generate[string](charNumber),
func(t string) string {
return strings.Split(availableCharacters, "")[integer.RandomBetween(0, len(availableCharacters)-1)]
},
),
"",
)
}
7 changes: 7 additions & 0 deletions auth/credentials/generateClientId.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package credentials

import "fmt"

func GenerateClientId() string {
return fmt.Sprintf("fs_service_i@%v", CreateNewToken(AvailableCharacters, 15))
}
7 changes: 7 additions & 0 deletions auth/credentials/generateClientSecret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package credentials

import "fmt"

func GenerateClientSecret() string {
return fmt.Sprintf("fs_service_s@%v", CreateNewToken(AvailableCharacters, 15))
}
48 changes: 48 additions & 0 deletions auth/credentials/generateCredentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package credentials

import (
"filesystem_service/auth/roles"
"filesystem_service/database"
"fmt"
)

func GenerateCredentials(role string) {
db, err := database.Init()
defer db.Close()
if err != nil {
fmt.Printf("Une erreur est survenue lors de l'initialisation de la base de donnée.\n")
fmt.Printf(err.Error() + "\n")
return
}

roleId := roles.GetRoleIdFromName(db, role)

if roleId != -1 {
fmt.Printf("Generation du client_id ...\n")
fmt.Printf("Generation du client_secret ...\n")
clientId := GenerateClientId()
clientSecret := GenerateClientSecret()

if _, err = db.Exec(`UPDATE credentials
SET active = FALSE
WHERE active = TRUE;`); err != nil {
fmt.Printf("\nUne erreur est survenue lors de la création du token de signature.\n")
fmt.Printf(err.Error() + "\n")
return
}

if _, err = db.Exec(`INSERT INTO credentials (
client_id,
client_secret,
role,
active
) VALUES (?, ?, ?, TRUE);`, clientId, clientSecret, roleId); err != nil {
fmt.Printf("\nUne erreur est survenue lors de la création du token de signature.\n")
fmt.Printf(err.Error() + "\n")
return
}

fmt.Printf("\nVous devrez les saisir dans le système d'exploitation web.\n")
fmt.Printf("\n=> client_id: %v\n=> client_secret: %v\n", clientId, clientSecret)
}
}
3 changes: 3 additions & 0 deletions auth/credentials/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package credentials

var AvailableCharacters = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789"
40 changes: 40 additions & 0 deletions auth/credentials/updateCredentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package credentials

import (
"filesystem_service/auth/roles"
"filesystem_service/database"
"fmt"
)

func UpdateCredentials(clientId string, newRole string) {
if clientId == "" {
fmt.Printf("Vous devez renseigner le client_id.\n")
return
}

if newRole == "" {
fmt.Printf("Vous devez renseigner votre nouveau rôle.\n")
return
}

db, err := database.Init()
defer db.Close()
if err != nil {
fmt.Printf("Une erreur est survenue lors de l'initialisation de la base de donnée.\n")
fmt.Printf(err.Error() + "\n")
return
}

newRoleId := roles.GetRoleIdFromName(db, newRole)

if _, err = db.Exec(
`UPDATE credentials SET role = ? WHERE client_id = ?;`,
newRoleId, clientId,
); err != nil {
fmt.Printf("Une erreur est survenue lors de laa modification de votre rôle.\n")
fmt.Printf(err.Error() + "\n")
return
}

fmt.Printf("Votre rôle à bien été modifié.\n")
}
69 changes: 0 additions & 69 deletions auth/generateSignatureToken.go

This file was deleted.

10 changes: 0 additions & 10 deletions auth/generateToken.go

This file was deleted.

Loading

0 comments on commit f04423c

Please sign in to comment.