github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/api/license.go (about) 1 /// Copyright 2017 European Digital Reading Lab. All rights reserved. 2 // Licensed to the Readium Foundation under one or more contributor license agreements. 3 // Use of this source code is governed by a BSD-style license 4 // that can be found in the LICENSE file exposed on Github (readium) in the project repository. 5 6 package staticapi 7 8 import ( 9 "encoding/json" 10 "log" 11 "net/http" 12 13 "github.com/gorilla/mux" 14 "github.com/readium/readium-lcp-server/api" 15 "github.com/readium/readium-lcp-server/frontend/webpublication" 16 "github.com/readium/readium-lcp-server/frontend/webpurchase" 17 "github.com/readium/readium-lcp-server/problem" 18 ) 19 20 // GetFilteredLicenses searches licenses activated by more than n devices 21 func GetFilteredLicenses(w http.ResponseWriter, r *http.Request, s IServer) { 22 23 rDevices := r.FormValue("devices") 24 log.Println("Licenses used by " + rDevices + " devices") 25 if rDevices == "" { 26 rDevices = "0" 27 } 28 29 if lic, err := s.LicenseAPI().GetFiltered(rDevices); err == nil { 30 w.Header().Set("Content-Type", api.ContentType_JSON) 31 enc := json.NewEncoder(w) 32 if err = enc.Encode(lic); err != nil { 33 problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError) 34 } 35 } else { 36 switch err { 37 case webpublication.ErrNotFound: 38 { 39 problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusNotFound) 40 } 41 default: 42 { 43 problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError) 44 } 45 } 46 } 47 } 48 49 // GetLicense gets an existing license by its id (passed as a section of the REST URL). 50 // It generates a partial license from the purchase info, 51 // fetches the license from the lcp server and returns it to the caller. 52 // This API method is called from a link in the license status document. 53 func GetLicense(w http.ResponseWriter, r *http.Request, s IServer) { 54 55 var purchase webpurchase.Purchase 56 var err error 57 58 vars := mux.Vars(r) 59 // get the license id in the URL 60 var licenseID = vars["license_id"] 61 if purchase, err = s.PurchaseAPI().GetByLicenseID(licenseID); err != nil { 62 switch err { 63 case webpurchase.ErrNotFound: 64 problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusNotFound) 65 default: 66 problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError) 67 } 68 return 69 } 70 // get an existing license from the lcp server 71 fullLicense, err := s.PurchaseAPI().GenerateOrGetLicense(purchase) 72 if err != nil { 73 problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError) 74 return 75 } 76 // return a json payload 77 w.Header().Set("Content-Type", api.ContentType_LCP_JSON) 78 // the file name is license.lcpl 79 w.Header().Set("Content-Disposition", "attachment; filename=\"license.lcpl\"") 80 81 // returns the full license to the caller 82 enc := json.NewEncoder(w) 83 // do not escape characters 84 enc.SetEscapeHTML(false) 85 err = enc.Encode(fullLicense) 86 if err != nil { 87 problem.Error(w, r, problem.Problem{Detail: err.Error()}, http.StatusInternalServerError) 88 return 89 } 90 // message to the console 91 //log.Println("Get license / id " + licenseID + " / " + purchase.Publication.Title + " / purchase " + strconv.FormatInt(purchase.ID, 10)) 92 }