11package main
22
33import (
4- "bytes"
54 "context"
65 "encoding/base64"
76 "encoding/json"
@@ -28,6 +27,7 @@ import (
2827
2928 "github.com/authgear/authgear-once-license-server/pkg/emailtemplate"
3029 "github.com/authgear/authgear-once-license-server/pkg/httpmiddleware"
30+ "github.com/authgear/authgear-once-license-server/pkg/installationscript"
3131 "github.com/authgear/authgear-once-license-server/pkg/keygen"
3232 "github.com/authgear/authgear-once-license-server/pkg/slogging"
3333 "github.com/authgear/authgear-once-license-server/pkg/smtp"
@@ -48,8 +48,6 @@ const indexHTML = `<!DOCTYPE html>
4848</html>
4949`
5050
51- var installationShellScript * texttemplate.Template
52-
5351var jsonResponseBadRequest = map [string ]any {
5452 "error" : map [string ]any {
5553 "code" : "bad_request" ,
@@ -82,32 +80,6 @@ var jsonResponseLicenseKeyExpired = map[string]any{
8280
8381var jsonResponseOK = map [string ]any {}
8482
85- func init () {
86- t , err := texttemplate .New ("" ).Parse (`#!/bin/sh
87- set -e
88-
89- echo "Installing the Authgear ONCE command......"
90- echo "This script uses sudo, you will be prompted for authentication."
91- sudo true
92-
93- download_url="{{ $.DownloadURL }}?uname_s=$(uname -s)&uname_m=$(uname -m)"
94- tmp_path="$(mktemp)"
95- curl -fsSL "$download_url" > "$tmp_path"
96- sudo mv "$tmp_path" /usr/local/bin/authgear-once
97- sudo chmod u+x /usr/local/bin/authgear-once
98-
99- if [ "$(uname -s)" = "Darwin" ]; then
100- /usr/local/bin/authgear-once setup "{{ $.LicenseKey }}"
101- else
102- sudo /usr/local/bin/authgear-once setup "{{ $.LicenseKey }}"
103- fi
104- ` )
105- if err != nil {
106- panic (err )
107- }
108- installationShellScript = t
109- }
110-
11183var rootCmd = & cobra.Command {
11284 Use : "authgear-once-license-server" ,
11385}
@@ -148,12 +120,11 @@ var serveCmd = &cobra.Command{
148120
149121 u := ConstructFullURL (r )
150122
151- data := map [string ]any {
152- "DownloadURL" : u .String (),
153- "LicenseKey" : licenseKey ,
154- }
155- var buf bytes.Buffer
156- err = installationShellScript .Execute (& buf , data )
123+ script , err := installationscript .Render (installationscript.RenderOptions {
124+ DownloadURL : u .String (),
125+ LicenseKey : licenseKey ,
126+ ImageOverride : deps .AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE ,
127+ })
157128 if err != nil {
158129 slogging .Error (ctx , logger , "failed to render installation shell script" ,
159130 "error" , err )
@@ -163,17 +134,17 @@ var serveCmd = &cobra.Command{
163134
164135 w .Header ().Set ("Content-Type" , "text/plain" )
165136 w .Header ().Set ("Cache-Control" , "no-store" )
166- w .Write (buf . Bytes ( ))
137+ w .Write ([] byte ( script ))
167138 default :
168- // Otherwise, we return 303 to download the binary .
139+ // Otherwise, we return 303 to download the executable .
169140 uname_s = uname .NormalizeUnameS (uname_s )
170141 uname_m = uname .NormalizeUnameM (uname_m )
171142 data := map [string ]any {
172143 "Uname_s" : uname_s ,
173144 "Uname_m" : uname_m ,
174145 }
175146 var buf strings.Builder
176- err = deps .AuthgearOnceDownloadURLGoTemplate .Execute (& buf , data )
147+ err = deps .AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE .Execute (& buf , data )
177148 if err != nil {
178149 slogging .Error (ctx , logger , "failed to render download url" ,
179150 "error" , err )
@@ -418,24 +389,25 @@ type dependenciesKeyType struct{}
418389var dependenciesKey = dependenciesKeyType {}
419390
420391type Dependencies struct {
421- HTTPClient * http.Client
422- StripeClient * client.API
423- SMTPDialer * gomail.Dialer
424- SMTPSender string
425- StripeCheckoutSessionSuccessURL string
426- StripeCheckoutSessionCancelURL string
427- StripeCheckoutSessionPriceID string
428- StripeWebhookSigningSecret string
429- PublicURLScheme string
430- AuthgearOnceDownloadURLGoTemplate * texttemplate.Template
431- KeygenConfig keygen.KeygenConfig
392+ HTTPClient * http.Client
393+ StripeClient * client.API
394+ SMTPDialer * gomail.Dialer
395+ SMTPSender string
396+ StripeCheckoutSessionSuccessURL string
397+ StripeCheckoutSessionCancelURL string
398+ StripeCheckoutSessionPriceID string
399+ StripeWebhookSigningSecret string
400+ AUTHGEAR_ONCE_PUBLIC_URL_SCHEME string
401+ AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE * texttemplate.Template
402+ AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE string
403+ KeygenConfig keygen.KeygenConfig
432404}
433405
434406func ConstructFullURL (r * http.Request ) * url.URL {
435407 ctx := r .Context ()
436408 deps := GetDependencies (ctx )
437409 scheme := "https"
438- if deps .PublicURLScheme == "http" {
410+ if deps .AUTHGEAR_ONCE_PUBLIC_URL_SCHEME == "http" {
439411 scheme = "http"
440412 }
441413 host := r .Host
@@ -491,22 +463,24 @@ func main() {
491463 SMTPPassword : os .Getenv ("AUTHGEAR_ONCE_SMTP_PASSWORD" ),
492464 })
493465
494- authgearOnceDownloadURLGoTemplate , err := texttemplate .New ("" ).Parse (os .Getenv ("AUTHGEAR_ONCE_BINARY_DOWNLOAD_URL_GO_TEMPLATE " ))
466+ AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE , err := texttemplate .New ("" ).Parse (os .Getenv ("AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE " ))
495467 if err != nil {
496468 panic (err )
497469 }
470+ AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE := os .Getenv ("AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE" )
498471
499472 dependencies := Dependencies {
500- HTTPClient : & http.Client {},
501- StripeClient : stripeClient ,
502- SMTPDialer : smtpDialer ,
503- SMTPSender : os .Getenv ("AUTHGEAR_ONCE_SMTP_SENDER" ),
504- StripeCheckoutSessionSuccessURL : os .Getenv ("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_SUCCESS_URL" ),
505- StripeCheckoutSessionCancelURL : os .Getenv ("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_CANCEL_URL" ),
506- StripeCheckoutSessionPriceID : os .Getenv ("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_PRICE_ID" ),
507- StripeWebhookSigningSecret : os .Getenv ("AUTHGEAR_ONCE_STRIPE_WEBHOOK_SIGNING_SECRET" ),
508- PublicURLScheme : os .Getenv ("AUTHGEAR_ONCE_PUBLIC_URL_SCHEME" ),
509- AuthgearOnceDownloadURLGoTemplate : authgearOnceDownloadURLGoTemplate ,
473+ HTTPClient : & http.Client {},
474+ StripeClient : stripeClient ,
475+ SMTPDialer : smtpDialer ,
476+ SMTPSender : os .Getenv ("AUTHGEAR_ONCE_SMTP_SENDER" ),
477+ StripeCheckoutSessionSuccessURL : os .Getenv ("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_SUCCESS_URL" ),
478+ StripeCheckoutSessionCancelURL : os .Getenv ("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_CANCEL_URL" ),
479+ StripeCheckoutSessionPriceID : os .Getenv ("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_PRICE_ID" ),
480+ StripeWebhookSigningSecret : os .Getenv ("AUTHGEAR_ONCE_STRIPE_WEBHOOK_SIGNING_SECRET" ),
481+ AUTHGEAR_ONCE_PUBLIC_URL_SCHEME : os .Getenv ("AUTHGEAR_ONCE_PUBLIC_URL_SCHEME" ),
482+ AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE : AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE ,
483+ AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE : AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE ,
510484 KeygenConfig : keygen.KeygenConfig {
511485 Endpoint : os .Getenv ("AUTHGEAR_ONCE_KEYGEN_ENDPOINT" ),
512486 AdminToken : os .Getenv ("AUTHGEAR_ONCE_KEYGEN_ADMIN_TOKEN" ),
0 commit comments