Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract the username and password from the mirror url #7651

Merged
merged 15 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion models/repo_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package models

import (
"fmt"
"net/url"
"strings"
"time"

Expand Down Expand Up @@ -118,7 +119,7 @@ func sanitizeOutput(output, repoPath string) (string, error) {
return util.SanitizeMessage(output, remoteAddr), nil
}

// Address returns mirror address from Git repository config without credentials.
// Address returns mirror address from Git repository config with credentials censored.
func (m *Mirror) Address() string {
m.readAddress()
return util.SanitizeURLCredentials(m.address, false)
Expand All @@ -130,6 +131,41 @@ func (m *Mirror) FullAddress() string {
return m.address
}

// AddressNoCredentials returns mirror address from Git repository config without credentials.
func (m *Mirror) AddressNoCredentials() string {
m.readAddress()
u, err := url.Parse(m.address)
if err != nil {
// this shouldn't happen but just return it unsanitised
return m.address
}
u.User = nil
return u.String()
}

// Username returns the mirror address username
func (m *Mirror) Username() string {
m.readAddress()
u, err := url.Parse(m.address)
if err != nil {
// this shouldn't happen but if it does return ""
return ""
}
return u.User.Username()
}

// Password returns the mirror address password
func (m *Mirror) Password() string {
m.readAddress()
u, err := url.Parse(m.address)
if err != nil {
// this shouldn't happen but if it does return ""
return ""
}
password, _ := u.User.Password()
return password
}

// SaveAddress writes new address to Git repository config.
func (m *Mirror) SaveAddress(addr string) error {
repoPath := m.Repo.RepoPath()
Expand Down
16 changes: 9 additions & 7 deletions modules/auth/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ func (f MigrateRepoForm) ParseRemoteAddr(user *models.User) (string, error) {

// RepoSettingForm form for changing repository settings
type RepoSettingForm struct {
RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
Description string `binding:"MaxSize(255)"`
Website string `binding:"ValidUrl;MaxSize(255)"`
Interval string
MirrorAddress string
Private bool
EnablePrune bool
RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
Description string `binding:"MaxSize(255)"`
Website string `binding:"ValidUrl;MaxSize(255)"`
Interval string
MirrorAddress string
MirrorUsername string
MirrorPassword string
Private bool
EnablePrune bool

// Advanced settings
EnableWiki bool
Expand Down
2 changes: 1 addition & 1 deletion options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ mirror_prune_desc = Remove obsolete remote-tracking references
mirror_interval = Mirror Interval (valid time units are 'h', 'm', 's'). 0 to disable automatic sync.
mirror_interval_invalid = The mirror interval is not valid.
mirror_address = Clone From URL
mirror_address_desc = Include any required authorization credentials in the URL. These must be url escaped as appropriate
mirror_address_desc = Put any required credentials in the Clone Authorization section.
mirror_address_url_invalid = The provided url is invalid. You must escape all components of the url correctly.
mirror_address_protocol_invalid = The provided url is invalid. Only http(s):https:// or git:https:// locations can be mirrored from.
mirror_last_synced = Last Synchronized
Expand Down
4 changes: 4 additions & 0 deletions routers/repo/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
return
}

if form.MirrorUsername != "" || form.MirrorPassword != "" {
u.User = url.UserPassword(form.MirrorUsername, form.MirrorPassword)
}

// Now use xurls
address := validFormAddress.FindString(form.MirrorAddress)
if address != form.MirrorAddress && form.MirrorAddress != "" {
Expand Down
19 changes: 18 additions & 1 deletion templates/repo/settings/options.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,26 @@
</div>
<div class="field {{if .Err_MirrorAddress}}error{{end}}">
<label for="mirror_address">{{.i18n.Tr "repo.mirror_address"}}</label>
<input id="mirror_address" name="mirror_address" value="{{.Mirror.FullAddress}}" required>
<input id="mirror_address" name="mirror_address" value="{{.Mirror.AddressNoCredentials}}" required>
<p class="help">{{.i18n.Tr "repo.mirror_address_desc"}}</p>
</div>
<div class="ui accordion optional field">
<label class="ui title {{if .Err_Auth}}text red active{{end}}">
<i class="icon dropdown"></i>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would remove that <i> as the section is not expandable

Copy link
Member

@silverwind silverwind Aug 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or actually find out why semantic-ui does not make it toggleable, something in the HTML I assume.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the issue is that the HTML is unbalanced. The <label for=""> tag below should probably be closed on the same line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heya! I have no idea how that unbalanced label sneaked in.

So the idea of sticking a label in there was to keep the formatting of collapsable section title the same - it looked weird to my eyes when it was different to all the other bits. I'll have to see if closing that label has changed that back.

<label for="">{{.i18n.Tr "repo.need_auth"}}
zeripath marked this conversation as resolved.
Show resolved Hide resolved
</label>
<div class="content {{if .Err_Auth}}active{{else if .Mirror.Username}}active{{end}}">
<div class="inline field {{if .Err_Auth}}error{{end}}">
<label for="mirror_username">{{.i18n.Tr "username"}}</label>
<input id="mirror_username" name="mirror_username" value="{{.Mirror.Username}}" {{if not .mirror_username}}data-need-clear="true"{{end}}>
</div>
<input class="fake" type="password">
<div class="inline field {{if .Err_Auth}}error{{end}}">
<label for="mirror_password">{{.i18n.Tr "password"}}</label>
<input id="mirror_password" name="mirror_password" type="password" value="{{.Mirror.Password}}" {{if not .mirror_password}}data-need-clear="true"{{end}}>
</div>
</div>
</div>

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