Skip to content

Commit bee1227

Browse files
authored
Extract the username and password from the mirror url (#7651)
* Explode out mirror username and password * Update models/repo_mirror.go * Just roundtrip the password * remove unused declaration * Update templates/repo/settings/options.tmpl
1 parent 867f46f commit bee1227

File tree

5 files changed

+69
-10
lines changed

5 files changed

+69
-10
lines changed

models/repo_mirror.go

+37-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package models
77

88
import (
99
"fmt"
10+
"net/url"
1011
"strings"
1112
"time"
1213

@@ -119,7 +120,7 @@ func sanitizeOutput(output, repoPath string) (string, error) {
119120
return util.SanitizeMessage(output, remoteAddr), nil
120121
}
121122

122-
// Address returns mirror address from Git repository config without credentials.
123+
// Address returns mirror address from Git repository config with credentials censored.
123124
func (m *Mirror) Address() string {
124125
m.readAddress()
125126
return util.SanitizeURLCredentials(m.address, false)
@@ -131,6 +132,41 @@ func (m *Mirror) FullAddress() string {
131132
return m.address
132133
}
133134

135+
// AddressNoCredentials returns mirror address from Git repository config without credentials.
136+
func (m *Mirror) AddressNoCredentials() string {
137+
m.readAddress()
138+
u, err := url.Parse(m.address)
139+
if err != nil {
140+
// this shouldn't happen but just return it unsanitised
141+
return m.address
142+
}
143+
u.User = nil
144+
return u.String()
145+
}
146+
147+
// Username returns the mirror address username
148+
func (m *Mirror) Username() string {
149+
m.readAddress()
150+
u, err := url.Parse(m.address)
151+
if err != nil {
152+
// this shouldn't happen but if it does return ""
153+
return ""
154+
}
155+
return u.User.Username()
156+
}
157+
158+
// Password returns the mirror address password
159+
func (m *Mirror) Password() string {
160+
m.readAddress()
161+
u, err := url.Parse(m.address)
162+
if err != nil {
163+
// this shouldn't happen but if it does return ""
164+
return ""
165+
}
166+
password, _ := u.User.Password()
167+
return password
168+
}
169+
134170
// SaveAddress writes new address to Git repository config.
135171
func (m *Mirror) SaveAddress(addr string) error {
136172
repoPath := m.Repo.RepoPath()

modules/auth/repo_form.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,15 @@ func (f MigrateRepoForm) ParseRemoteAddr(user *models.User) (string, error) {
9898

9999
// RepoSettingForm form for changing repository settings
100100
type RepoSettingForm struct {
101-
RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
102-
Description string `binding:"MaxSize(255)"`
103-
Website string `binding:"ValidUrl;MaxSize(255)"`
104-
Interval string
105-
MirrorAddress string
106-
Private bool
107-
EnablePrune bool
101+
RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
102+
Description string `binding:"MaxSize(255)"`
103+
Website string `binding:"ValidUrl;MaxSize(255)"`
104+
Interval string
105+
MirrorAddress string
106+
MirrorUsername string
107+
MirrorPassword string
108+
Private bool
109+
EnablePrune bool
108110

109111
// Advanced settings
110112
EnableWiki bool

options/locale/locale_en-US.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ mirror_prune_desc = Remove obsolete remote-tracking references
584584
mirror_interval = Mirror Interval (valid time units are 'h', 'm', 's'). 0 to disable automatic sync.
585585
mirror_interval_invalid = The mirror interval is not valid.
586586
mirror_address = Clone From URL
587-
mirror_address_desc = Include any required authorization credentials in the URL. These must be url escaped as appropriate
587+
mirror_address_desc = Put any required credentials in the Clone Authorization section.
588588
mirror_address_url_invalid = The provided url is invalid. You must escape all components of the url correctly.
589589
mirror_address_protocol_invalid = The provided url is invalid. Only http(s):// or git:// locations can be mirrored from.
590590
mirror_last_synced = Last Synchronized

routers/repo/setting.go

+4
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
169169
return
170170
}
171171

172+
if form.MirrorUsername != "" || form.MirrorPassword != "" {
173+
u.User = url.UserPassword(form.MirrorUsername, form.MirrorPassword)
174+
}
175+
172176
// Now use xurls
173177
address := validFormAddress.FindString(form.MirrorAddress)
174178
if address != form.MirrorAddress && form.MirrorAddress != "" {

templates/repo/settings/options.tmpl

+18-1
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,26 @@
8080
</div>
8181
<div class="field {{if .Err_MirrorAddress}}error{{end}}">
8282
<label for="mirror_address">{{.i18n.Tr "repo.mirror_address"}}</label>
83-
<input id="mirror_address" name="mirror_address" value="{{.Mirror.FullAddress}}" required>
83+
<input id="mirror_address" name="mirror_address" value="{{.Mirror.AddressNoCredentials}}" required>
8484
<p class="help">{{.i18n.Tr "repo.mirror_address_desc"}}</p>
8585
</div>
86+
<div class="ui accordion optional field">
87+
<label class="ui title {{if .Err_Auth}}text red active{{end}}">
88+
<i class="icon dropdown"></i>
89+
<label for="">{{.i18n.Tr "repo.need_auth"}}</label>
90+
</label>
91+
<div class="content {{if .Err_Auth}}active{{else if .Mirror.Username}}active{{end}}">
92+
<div class="inline field {{if .Err_Auth}}error{{end}}">
93+
<label for="mirror_username">{{.i18n.Tr "username"}}</label>
94+
<input id="mirror_username" name="mirror_username" value="{{.Mirror.Username}}" {{if not .mirror_username}}data-need-clear="true"{{end}}>
95+
</div>
96+
<input class="fake" type="password">
97+
<div class="inline field {{if .Err_Auth}}error{{end}}">
98+
<label for="mirror_password">{{.i18n.Tr "password"}}</label>
99+
<input id="mirror_password" name="mirror_password" type="password" value="{{.Mirror.Password}}" {{if not .mirror_password}}data-need-clear="true"{{end}}>
100+
</div>
101+
</div>
102+
</div>
86103

87104
<div class="field">
88105
<button class="ui green button">{{$.i18n.Tr "repo.settings.update_settings"}}</button>

0 commit comments

Comments
 (0)