@@ -109,16 +109,19 @@ func (this TemplateRegistry) GetTemplatePath(name string) (string, error) {
109109 Registers the given [path] in the registry, by copying the archive file to it.
110110 If the file is not an archive, or it cannot be read, or the registry cannot be written,
111111 an error is returned.
112+ If the given [path] is remote (such as http), and if the server requires authentication,
113+ the given [username] and [password] will be used.
114+ If the path is local, username/password are inconsequential.
112115*/
113- func (this * TemplateRegistry ) RegisterTemplate (path string ) (string , error ) {
116+ func (this * TemplateRegistry ) RegisterTemplate (path string , username string , password string ) (string , error ) {
114117
115118 var targetPath , name string
116119 var err error
117120
118121 // if this is a remote path, download as a zip to a temporary directory before trying to register.
119122 if strings .HasPrefix (path , "http://" ) || strings .HasPrefix (path , "https://" ) {
120123
121- path , err = downloadArchive (path )
124+ path , err = downloadArchive (path , username , password )
122125 if (err != nil ) {
123126 return "" , err
124127 }
@@ -145,9 +148,11 @@ func (this *TemplateRegistry) RegisterTemplate(path string) (string, error) {
145148 Downloads the given [url] to a temporary directory (as a .zip).
146149 Returns the temporary path to the downloaded archive, or an error if it encountered one.
147150*/
148- func downloadArchive (targetURL string ) (string , error ) {
151+ func downloadArchive (targetURL string , username string , password string ) (string , error ) {
149152
153+ var request * http.Request
150154 var response * http.Response
155+ var client http.Client
151156 var outputFile * os.File
152157 var parsedURL * url.URL
153158 var outputPath string
@@ -164,6 +169,12 @@ func downloadArchive(targetURL string) (string, error) {
164169 return "" , err
165170 }
166171
172+ request , err = http .NewRequest ("GET" , targetURL , nil )
173+ if (err != nil ) {
174+ return "" , err
175+ }
176+ request .SetBasicAuth (username , password )
177+
167178 baseName = filepath .Base (parsedURL .Path )
168179 baseName = strings .TrimSuffix (baseName , filepath .Ext (baseName ))
169180 outputPath = filepath .Join (outputPath , baseName + ".zip" )
@@ -175,12 +186,12 @@ func downloadArchive(targetURL string) (string, error) {
175186 }
176187 defer outputFile .Close ()
177188
178- response , err = http . Get ( targetURL )
189+ response , err = client . Do ( request )
179190 if (err != nil ) {
180191 return "" , err
181192 }
182193
183- if (response .Status != "200" ) {
194+ if (! strings . Contains ( response .Status , "200" ) ) {
184195 errorMsg := fmt .Sprintf ("Unable to download remote archive: HTTP %s\n " , response .Status )
185196 return "" , errors .New (errorMsg )
186197 }
0 commit comments