Skip to content

Commit 2922dae

Browse files
Refactor license endpoints
1 parent 5683f2d commit 2922dae

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ In the **example** directory, you can access to a simple usage of the activation
3838
1. Run `go build -o f-cli ./cli`
3939
2. Generate `license.json` like [sample_license.json](https://github.com/furkansenharputlu/f-license/blob/master/sample_license.json)
4040
3. Run `./f-cli generate license.json`
41-
4. Run `./f-cli -h` to see other commands
41+
4. Run `./f-cli verify <license-token>`
42+
5. Run `./f-cli -h` to see other commands

‎api.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ func ChangeLicenseActiveness(w http.ResponseWriter, r *http.Request) {
7777
}
7878

7979
func VerifyLicense(w http.ResponseWriter, r *http.Request) {
80-
license := r.FormValue("license")
80+
token := r.FormValue("token")
8181

8282
var l lcs.License
83-
err := l.GetByToken(license)
83+
err := l.GetByToken(token)
8484
if err != nil {
8585
logrus.WithError(err).Error("Error while getting license")
8686
ReturnError(w, err.Error())
8787
return
8888
}
8989

90-
ok, err := l.IsLicenseValid(license)
90+
ok, err := l.IsLicenseValid(token)
9191
if err != nil {
9292
ReturnResponse(w, http.StatusUnauthorized, map[string]interface{}{
9393
"valid": false,

‎lcs/license.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ func (l *License) Activate(id string, inactivate bool) error {
152152
return nil
153153
}
154154

155-
func (l *License) IsLicenseValid(license string) (bool, error) {
155+
func (l *License) IsLicenseValid(tokenString string) (bool, error) {
156156

157157
if !l.Active {
158158
return false, fmt.Errorf("license inactivated")
159159
}
160160

161-
token, err := jwt.Parse(license, func(token *jwt.Token) (interface{}, error) {
161+
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
162162
// Don't forget to validate the alg is what you expect:
163163
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
164164
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])

‎main.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ func main() {
2828
// Endpoints called by product owners
2929
adminRouter := r.PathPrefix("/admin").Subrouter()
3030
adminRouter.Use(AuthenticationMiddleware)
31-
adminRouter.HandleFunc("/generate", GenerateLicense).Methods(http.MethodPost)
32-
adminRouter.HandleFunc("/{id}", GetLicense).Methods(http.MethodGet)
33-
adminRouter.HandleFunc("/delete/{id}", DeleteLicense).Methods(http.MethodDelete)
34-
adminRouter.HandleFunc("/activate/{id}", ChangeLicenseActiveness).Methods(http.MethodPut)
35-
adminRouter.HandleFunc("/inactivate/{id}", ChangeLicenseActiveness).Methods(http.MethodPut)
31+
adminRouter.HandleFunc("/licenses", GenerateLicense).Methods(http.MethodPost)
32+
adminRouter.HandleFunc("/licenses/{id}", GetLicense).Methods(http.MethodGet)
33+
adminRouter.HandleFunc("/licenses/{id}/activate", ChangeLicenseActiveness).Methods(http.MethodPut)
34+
adminRouter.HandleFunc("/licenses/{id}/inactivate", ChangeLicenseActiveness).Methods(http.MethodPut)
35+
adminRouter.HandleFunc("/licenses/{id}/delete", DeleteLicense).Methods(http.MethodDelete)
3636

3737
// Endpoints called by product instances having license
3838
r.HandleFunc("/license/verify", VerifyLicense).Methods(http.MethodPost)

‎sample_license.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"claims": {
44
"username": "Furkan",
55
"address": "Istanbul, Turkey"
6-
}
6+
},
7+
"active": true
78
}

0 commit comments

Comments
 (0)