Skip to content

Commit 721d8f1

Browse files
committed
feat: added authentication in API and search api
Get license with specific search term: field: represents the name of field to be searched search_term: represents the term to be searched search: represent the algorithm to search with authenticate the API: added the struct for user created a user table in the database added basic user endpoints: get user using id get all user create user added basic authentication to all the API endpoints i.e, group of endpoints Signed-off-by: Kavya Shukla <[email protected]>
1 parent 750c059 commit 721d8f1

File tree

8 files changed

+377
-89
lines changed

8 files changed

+377
-89
lines changed

cmd/laas/main.go

+7-42
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,12 @@
44
package main
55

66
import (
7-
"encoding/json"
87
"flag"
9-
"fmt"
10-
"io/ioutil"
118
"log"
129

1310
"github.com/fossology/LicenseDb/pkg/api"
14-
"github.com/fossology/LicenseDb/pkg/auth"
11+
"github.com/fossology/LicenseDb/pkg/db"
1512
"github.com/fossology/LicenseDb/pkg/models"
16-
"github.com/fossology/LicenseDb/pkg/utils"
17-
"github.com/gin-gonic/gin"
18-
"gorm.io/driver/postgres"
19-
"gorm.io/gorm"
2013
)
2114

2215
// declare flags to input the basic requirement of database connection and the path of the data file
@@ -40,46 +33,18 @@ var (
4033
func main() {
4134
flag.Parse()
4235

43-
dburi := fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s", *dbhost, *port, *user, *dbname, *password)
44-
gormConfig := &gorm.Config{}
45-
database, err := gorm.Open(postgres.Open(dburi), gormConfig)
46-
if err != nil {
47-
log.Fatalf("Failed to connect to database: %v", err)
48-
}
36+
db.Connect(dbhost, port, user, dbname, password)
4937

50-
if err := database.AutoMigrate(&models.LicenseDB{}); err != nil {
38+
if err := db.DB.AutoMigrate(&models.LicenseDB{}); err != nil {
5139
log.Fatalf("Failed to automigrate database: %v", err)
5240
}
5341

54-
if err := database.AutoMigrate(&models.User{}); err != nil {
42+
if err := db.DB.AutoMigrate(&models.User{}); err != nil {
5543
log.Fatalf("Failed to automigrate database: %v", err)
5644
}
57-
if *populatedb {
58-
var licenses []models.LicenseJson
59-
// read the file of data
60-
byteResult, _ := ioutil.ReadFile(*datafile)
61-
// unmarshal the json file and it into the struct format
62-
if err := json.Unmarshal(byteResult, &licenses); err != nil {
63-
log.Fatalf("error reading from json file: %v", err)
64-
}
65-
for _, license := range licenses {
66-
// populate the data in the database table
67-
result := utils.Converter(license)
68-
database.Create(&result)
69-
}
70-
}
71-
api.DB = database
7245

73-
r := gin.Default()
74-
r.NoRoute(api.HandleInvalidUrl)
75-
authorized := r.Group("/")
76-
authorized.Use(auth.AuthenticationMiddleware())
77-
authorized.GET("/api/license/:shortname", api.GetLicense)
78-
authorized.POST("/api/license", api.CreateLicense)
79-
authorized.PATCH("/api/license/update/:shortname", api.UpdateLicense)
80-
authorized.GET("/api/licenses", api.SearchInLicense)
81-
r.POST("/api/user", auth.CreateUser)
82-
authorized.GET("/api/users", auth.GetAllUser)
83-
authorized.GET("/api/user/:id", auth.GetUser)
46+
db.Populatedb(*populatedb, *datafile)
47+
48+
r := api.Router()
8449
r.Run()
8550
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ go 1.20
44

55
require (
66
github.com/gin-gonic/gin v1.9.1
7+
github.com/stretchr/testify v1.8.3
78
gorm.io/driver/postgres v1.5.2
89
gorm.io/gorm v1.25.1
910
)
1011

1112
require (
1213
github.com/bytedance/sonic v1.9.1 // indirect
1314
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
15+
github.com/davecgh/go-spew v1.1.1 // indirect
1416
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
1517
github.com/gin-contrib/sse v0.1.0 // indirect
1618
github.com/go-playground/locales v0.14.1 // indirect
@@ -30,6 +32,7 @@ require (
3032
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3133
github.com/modern-go/reflect2 v1.0.2 // indirect
3234
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
35+
github.com/pmezard/go-difflib v1.0.0 // indirect
3336
github.com/rogpeppe/go-internal v1.10.0 // indirect
3437
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
3538
github.com/ugorji/go/codec v1.2.11 // indirect

pkg/api/api.go

+89-17
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,35 @@ import (
88
"net/http"
99
"time"
1010

11+
"github.com/fossology/LicenseDb/pkg/auth"
12+
"github.com/fossology/LicenseDb/pkg/db"
1113
"github.com/fossology/LicenseDb/pkg/models"
1214
"github.com/gin-gonic/gin"
13-
"gorm.io/gorm"
1415
)
1516

16-
var DB *gorm.DB
17+
func Router() *gin.Engine {
18+
// r is a default instance of gin engine
19+
r := gin.Default()
20+
21+
// return error for invalid routes
22+
r.NoRoute(HandleInvalidUrl)
23+
24+
// authorization not required for these routes
25+
r.GET("/api/license/:shortname", GetLicense)
26+
r.GET("/api/licenses", SearchInLicense)
27+
r.GET("/api/users", auth.GetAllUser)
28+
r.GET("/api/user/:id", auth.GetUser)
29+
30+
// set up authentication
31+
authorized := r.Group("/")
32+
authorized.Use(auth.AuthenticationMiddleware())
33+
34+
authorized.POST("/api/license", CreateLicense)
35+
authorized.PATCH("/api/license/update/:shortname", UpdateLicense)
36+
authorized.POST("/api/user", auth.CreateUser)
37+
38+
return r
39+
}
1740

1841
func HandleInvalidUrl(c *gin.Context) {
1942

@@ -27,9 +50,10 @@ func HandleInvalidUrl(c *gin.Context) {
2750
c.JSON(http.StatusNotFound, er)
2851
}
2952
func GetAllLicense(c *gin.Context) {
53+
3054
var licenses []models.LicenseDB
3155

32-
err := DB.Find(&licenses).Error
56+
err := db.DB.Find(&licenses).Error
3357
if err != nil {
3458
er := models.LicenseError{
3559
Status: http.StatusBadRequest,
@@ -44,7 +68,7 @@ func GetAllLicense(c *gin.Context) {
4468
res := models.LicenseResponse{
4569
Data: licenses,
4670
Status: http.StatusOK,
47-
Meta: models.Meta{
71+
Meta: models.PaginationMeta{
4872
ResourceCount: len(licenses),
4973
},
5074
}
@@ -60,7 +84,7 @@ func GetLicense(c *gin.Context) {
6084
return
6185
}
6286

63-
err := DB.Where("shortname = ?", queryParam).First(&license).Error
87+
err := db.DB.Where("shortname = ?", queryParam).First(&license).Error
6488

6589
if err != nil {
6690
er := models.LicenseError{
@@ -77,7 +101,7 @@ func GetLicense(c *gin.Context) {
77101
res := models.LicenseResponse{
78102
Data: []models.LicenseDB{license},
79103
Status: http.StatusOK,
80-
Meta: models.Meta{
104+
Meta: models.PaginationMeta{
81105
ResourceCount: 1,
82106
},
83107
}
@@ -105,7 +129,7 @@ func CreateLicense(c *gin.Context) {
105129
}
106130
license := models.LicenseDB(input)
107131

108-
result := DB.FirstOrCreate(&license)
132+
result := db.DB.FirstOrCreate(&license)
109133
if result.RowsAffected == 0 {
110134

111135
er := models.LicenseError{
@@ -132,7 +156,7 @@ func CreateLicense(c *gin.Context) {
132156
res := models.LicenseResponse{
133157
Data: []models.LicenseDB{license},
134158
Status: http.StatusCreated,
135-
Meta: models.Meta{
159+
Meta: models.PaginationMeta{
136160
ResourceCount: 1,
137161
},
138162
}
@@ -144,7 +168,7 @@ func UpdateLicense(c *gin.Context) {
144168
var update models.LicenseDB
145169
var license models.LicenseDB
146170
shortname := c.Param("shortname")
147-
if err := DB.Where("shortname = ?", shortname).First(&license).Error; err != nil {
171+
if err := db.DB.Where("shortname = ?", shortname).First(&license).Error; err != nil {
148172
er := models.LicenseError{
149173
Status: http.StatusBadRequest,
150174
Message: fmt.Sprintf("license with shortname '%s' not found", shortname),
@@ -166,7 +190,7 @@ func UpdateLicense(c *gin.Context) {
166190
c.JSON(http.StatusBadRequest, er)
167191
return
168192
}
169-
if err := DB.Model(&license).Updates(update).Error; err != nil {
193+
if err := db.DB.Model(&license).Updates(update).Error; err != nil {
170194
er := models.LicenseError{
171195
Status: http.StatusInternalServerError,
172196
Message: "Failed to update license",
@@ -180,7 +204,7 @@ func UpdateLicense(c *gin.Context) {
180204
res := models.LicenseResponse{
181205
Data: []models.LicenseDB{license},
182206
Status: http.StatusOK,
183-
Meta: models.Meta{
207+
Meta: models.PaginationMeta{
184208
ResourceCount: 1,
185209
},
186210
}
@@ -193,16 +217,62 @@ func SearchInLicense(c *gin.Context) {
193217
field := c.Query("field")
194218
search_term := c.Query("search_term")
195219
search := c.Query("search")
196-
if field == "" && search_term == "" {
220+
SpdxId := c.Query("spdxid")
221+
DetectorType := c.Query("detector_type")
222+
GPLv2compatible := c.Query("gplv2compatible")
223+
GPLv3compatible := c.Query("gplv3compatible")
224+
marydone := c.Query("marydone")
225+
active := c.Query("active")
226+
OSIapproved := c.Query("osiapproved")
227+
fsffree := c.Query("fsffree")
228+
copyleft := c.Query("copyleft")
229+
var license []models.LicenseDB
230+
query := db.DB.Model(&license)
231+
232+
if field == "" && search_term == "" && SpdxId == "" && GPLv2compatible == "" && GPLv3compatible == "" && DetectorType == "" && marydone == "" && active == "" && fsffree == "" && OSIapproved == "" && copyleft == "" {
197233
GetAllLicense(c)
198234
return
199235
}
200-
var query *gorm.DB
201-
var license []models.LicenseDB
236+
if active != "" {
237+
query = query.Where("active=?", active)
238+
}
239+
240+
if fsffree != "" {
241+
query = query.Where("fs_ffree=?", fsffree)
242+
}
243+
244+
if OSIapproved != "" {
245+
query = query.Where("os_iapproved=?", OSIapproved)
246+
}
247+
248+
if copyleft != "" {
249+
query = query.Where("copyleft=?", copyleft)
250+
}
251+
252+
if SpdxId != "" {
253+
query = query.Where("spdx_id=?", SpdxId)
254+
}
255+
256+
if DetectorType != "" {
257+
query = query.Where("detector_type=?", DetectorType)
258+
}
259+
260+
if GPLv2compatible != "" {
261+
query = query.Where("gp_lv2compatible=?", GPLv2compatible)
262+
}
263+
264+
if GPLv3compatible != "" {
265+
query = query.Where("gp_lv3compatible=?", GPLv3compatible)
266+
}
267+
268+
if marydone != "" {
269+
query = query.Where("marydone=?", marydone)
270+
}
271+
202272
if search == "fuzzy" {
203-
query = DB.Where(fmt.Sprintf("%s ILIKE ?", field), fmt.Sprintf("%%%s%%", search_term)).Find(&license)
273+
query = query.Where(fmt.Sprintf("%s ILIKE ?", field), fmt.Sprintf("%%%s%%", search_term))
204274
} else if search == "" || search == "full_text_search" {
205-
query = DB.Where(field+" @@ plainto_tsquery(?)", search_term).Find(&license)
275+
query = query.Where(field+" @@ plainto_tsquery(?)", search_term)
206276
} else {
207277
er := models.LicenseError{
208278
Status: http.StatusBadRequest,
@@ -226,10 +296,12 @@ func SearchInLicense(c *gin.Context) {
226296
c.JSON(http.StatusBadRequest, er)
227297
return
228298
}
299+
query.Find(&license)
300+
229301
res := models.LicenseResponse{
230302
Data: license,
231303
Status: http.StatusOK,
232-
Meta: models.Meta{
304+
Meta: models.PaginationMeta{
233305
ResourceCount: len(license),
234306
},
235307
}

0 commit comments

Comments
 (0)