87 lines
2.1 KiB
Go
87 lines
2.1 KiB
Go
package gosdk
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
// ── Type Definitions ─────────────────────────────────────────────
|
|
type Configuration struct {
|
|
BackendUrl string
|
|
Secret string
|
|
ServiceId string
|
|
BypassingUids []string
|
|
UseStubs bool
|
|
StubsDefault bool
|
|
}
|
|
|
|
type requestBody struct {
|
|
Secret string `json:"signingSecret"`
|
|
ServiceId string `json:"name"`
|
|
Uid string `json:"uid"`
|
|
ProductId string `json:"productID"`
|
|
ProductType string `json:"type"`
|
|
}
|
|
|
|
// ── Globals ──────────────────────────────────────────────────────
|
|
var conf *Configuration
|
|
var usingStubs bool
|
|
var failCountSinceLastSuccess int
|
|
|
|
// ── Functions ────────────────────────────────────────────────────
|
|
|
|
// Initialize the SDK, specifying the configuration
|
|
func Init(config Configuration) {
|
|
conf = &config
|
|
usingStubs = false
|
|
failCountSinceLastSuccess = 0
|
|
}
|
|
|
|
// Check if a given users owns a product. Make sure to always
|
|
func CheckOwned(uid string, productId string, productType string) bool {
|
|
if conf == nil {
|
|
panic("CheckOwned called prior to Store SDK initialization")
|
|
}
|
|
|
|
if usingStubs || conf.UseStubs {
|
|
return conf.StubsDefault
|
|
}
|
|
|
|
// Bypass check
|
|
for _, v := range conf.BypassingUids {
|
|
if v == uid {
|
|
return true
|
|
}
|
|
}
|
|
|
|
// Create request
|
|
data, err := json.Marshal(requestBody{
|
|
Secret: conf.Secret,
|
|
ServiceId: conf.ServiceId,
|
|
Uid: uid,
|
|
ProductId: productId,
|
|
ProductType: productType,
|
|
})
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
res, err := http.Post(conf.BackendUrl+"/sdk/api/check", "application/json", bytes.NewBuffer(data))
|
|
if err != nil {
|
|
if failCountSinceLastSuccess > 10 {
|
|
usingStubs = true
|
|
}
|
|
failCountSinceLastSuccess += 1
|
|
return false
|
|
}
|
|
failCountSinceLastSuccess = 0
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return string(body) == "true"
|
|
}
|