Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Give admins the ability to disable AppStore redirects #1466

Merged
merged 1 commit into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
73 changes: 52 additions & 21 deletions cmd/server/assets/mobileapps/_app.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,75 @@

{{$app := .app}}

<style>
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be in application.css?

Copy link
Member Author

Choose a reason for hiding this comment

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

Meh, if we end up using it across multiple files we can move it over.

.d-android-only, .d-app-none, .d-ios-only {
display: none;
}
</style>

<div class="form-label-group">
<input type="text" name="name" id="name" class="form-control{{if $app.ErrorsFor "name"}} is-invalid{{end}}" value="{{$app.Name}}"
placeholder="Application name" autofocus>
<label for="name">Application name</label>
{{template "errorable" $app.ErrorsFor "name"}}
<small class="form-text text-muted">
This is the name of your application.
</small>
</div>

<div class="form-label-group">
<input type="text" name="url" id="url" class="form-control{{if $app.ErrorsFor "url"}} is-invalid{{end}}" value="{{$app.URL}}"
placeholder="AppStore URL" autofocus>
<label for="url">AppStore URL</label>
{{template "errorable" $app.ErrorsFor "url"}}
<small class="form-text text-muted">
This is the URL where users can download your app. For iOS apps, this should
be a <a href="https://developer.apple.com/">Apple App Store URL</a>. For
Android apps, this should be a <a
href="https://support.google.com/admob/answer/3086746?hl=en">Google Play
Store URL</a>.
</small>
</div>

<div class="custom-control custom-switch mb-3">
<input type="checkbox" name="enable_redirect" id="enable-redirect"
class="custom-control-input" {{checkedIf (not $app.DisableRedirect)}}
{{if $app.ErrorsFor "disable_redirect"}}is-invalid{{end}}>
<label class="custom-control-label" for="enable-redirect">Enable AppStore redirects</label>
{{template "errorable" $app.ErrorsFor "disable_redirect"}}
<small class="form-text text-muted">
If a patient clicks on a verification link when they do not have the app
installed, the system will redirect them to the appropriate store to install
the application. We recommend keeping this option enabled to help increase
app adoption.
</small>
</div>

<hr>

<div class="form-group">
<select name="os" id="os" class="form-control custom-select{{if $app.ErrorsFor "os"}} is-invalid{{end}}">
<option selected disabled>Choose platform...</option>
<option value="{{.iOS}}" {{if (eq $app.OS .iOS)}}selected{{end}}>iOS Custom App</option>
<option value="{{.android}}" {{if (eq $app.OS .android)}}selected{{end}}>Android EN Express or Custom App</option>
<option value="{{.iOS}}" {{if (eq $app.OS .iOS)}}selected{{end}}>iOS custom app</option>
<option value="{{.android}}" {{if (eq $app.OS .android)}}selected{{end}}>Android EN Express or custom app</option>
</select>
{{template "errorable" $app.ErrorsFor "os"}}
</div>

<div id="app-id-group" class="form-label-group d-none">
<div id="app-id-group" class="form-label-group d-app-none">
<input type="text" name="app_id" id="app-id" class="form-control text-monospace{{if $app.ErrorsFor "app_id"}} is-invalid{{end}}" value="{{$app.AppID}}">
<label for="app-id" id="app-id-label"></label>
{{template "errorable" $app.ErrorsFor "app_id"}}
<small class="form-text text-muted d-none" id="ioshelp">
The iOS Mobile App configuration must be blank if you are an iOS Exposure Notifications Express (ENX) customer. This should only
be filled in for custom applications.
The iOS app id should be either a team ID or app ID prefix followed by the bundle ID. eg. ABCD1234.com.google.test.application
<small class="form-text text-muted d-ios-only">
The Application ID should be either a team ID or app ID prefix followed by
the bundle ID (e.g. <code>ABCD1234.com.my.app</code>).
</small>
<small class="form-text text-muted d-android-only">
The Package Name is the registered Android package name (e.g. <code>com.example.my.app</code>)
</small>
</div>

<div id="sha-group" class="form-label-group d-none">
<div id="sha-group" class="form-label-group d-android-only">
<textarea name="sha" id="sha" class="form-control text-monospace{{if $app.ErrorsFor "sha"}} is-invalid{{end}}"
placeholder="SHA256s (one per line)" rows="5">{{$app.SHA}}</textarea>
<label for="sha">SHA256 from the app signing certificate (one per line) </label>
Expand All @@ -54,29 +88,26 @@
<script type="text/javascript">
$(function() {
let $selectOS = $('select#os');
let $appID = $('#app-id');
let $appIDGroup = $('#app-id-group');
let $appIDInput = $('#app-id');
let $appIDLabel = $('#app-id-label');
let $sha = $('#sha');
let $shaGroup = $('#sha-group');
let $iosHelp = $('#ioshelp');

function processChange() {
$appIDGroup.addClass('d-none');
$appIDGroup.hide();

let val = $selectOS.children('option:selected').val();
if (val === '{{.iOS}}') {
$appID.attr('placeholder', 'Application ID');
$appIDInput.attr('placeholder', 'Application ID');
$appIDLabel.html('Application ID');
$shaGroup.addClass('d-none');
$appIDGroup.removeClass('d-none');
$iosHelp.removeClass('d-none');
$('.d-android-only').hide();
$('.d-ios-only').show();
$appIDGroup.show();
} else if (val === '{{.android}}') {
$appID.attr('placeholder', 'Package name');
$appIDInput.attr('placeholder', 'Package name');
$appIDLabel.html('Package name');
$shaGroup.removeClass('d-none');
$appIDGroup.removeClass('d-none');
$iosHelp.addClass('d-none');
$('.d-android-only').show();
$('.d-ios-only').hide();
$appIDGroup.show();
}
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/server/assets/mobileapps/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ <h1>{{$app.Name}}</h1>
<dt>AppStore link</dt>
<dd id="mobileapps-url"><a href="{{$app.URL}}" rel="noreferrer" target="_blank">{{$app.URL}}</a></dd>

<dt>Enable AppStore redirect</dt>
<dd>{{not $app.DisableRedirect}}</dd>

<dt>OS</dt>
<dd id="mobileapps-os">
{{if (eq $app.OS .iOS)}}
Expand Down
12 changes: 7 additions & 5 deletions pkg/controller/mobileapps/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,19 @@ func (c *Controller) HandleCreate() http.Handler {

func bindCreateForm(r *http.Request, app *database.MobileApp) error {
type FormData struct {
Name string `form:"name"`
URL string `form:"url"`
OS database.OSType `form:"os"`
AppID string `form:"app_id"`
SHA string `form:"sha"`
Name string `form:"name"`
URL string `form:"url"`
EnableRedirect bool `form:"enable_redirect"`
OS database.OSType `form:"os"`
AppID string `form:"app_id"`
SHA string `form:"sha"`
}

var form FormData
err := controller.BindForm(nil, r, &form)
app.Name = form.Name
app.URL = form.URL
app.DisableRedirect = !form.EnableRedirect
app.OS = form.OS
app.AppID = form.AppID
app.SHA = form.SHA
Expand Down
12 changes: 7 additions & 5 deletions pkg/controller/mobileapps/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,19 @@ func (c *Controller) HandleUpdate() http.Handler {

func bindUpdateForm(r *http.Request, app *database.MobileApp) error {
type FormData struct {
Name string `form:"name"`
URL string `form:"url"`
OS database.OSType `form:"os"`
AppID string `form:"app_id"`
SHA string `form:"sha"`
Name string `form:"name"`
URL string `form:"url"`
EnableRedirect bool `form:"enable_redirect"`
OS database.OSType `form:"os"`
AppID string `form:"app_id"`
SHA string `form:"sha"`
}

var form FormData
err := controller.BindForm(nil, r, &form)
app.Name = form.Name
app.URL = form.URL
app.DisableRedirect = !form.EnableRedirect
app.OS = form.OS
app.AppID = form.AppID
app.SHA = form.SHA
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/mobileapps/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func TestHandleUpdate(t *testing.T) {
chromedp.WaitVisible(`body#mobileapps-edit`, chromedp.ByQuery),

chromedp.SetValue(`input#name`, "Updated name", chromedp.ByQuery),
chromedp.RemoveAttribute(`input#enable-redirect`, "checked", chromedp.ByQuery),
chromedp.Click(`#submit`, chromedp.ByQuery),

chromedp.WaitVisible(`body#mobileapps-show`, chromedp.ByQuery),
Expand All @@ -198,5 +199,8 @@ func TestHandleUpdate(t *testing.T) {
if got, want := record.Name, "Updated name"; got != want {
t.Errorf("expected %q to be %q", got, want)
}
if got, want := record.DisableRedirect, true; got != want {
t.Errorf("expected %t to be %t", got, want)
}
})
}
18 changes: 15 additions & 3 deletions pkg/controller/redirect/appstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ func (c *Controller) getAppStoreData(realmID uint) (*AppStoreData, error) {
return nil, fmt.Errorf("failed to get Android Apps: %w", err)
}
if len(androidApps) > 0 {
androidURL = androidApps[0].URL
androidAppID = androidApps[0].AppID
// Find the first app that has redirects enabled.
for _, app := range androidApps {
if !app.DisableRedirect {
androidURL = app.URL
androidAppID = app.AppID
break
}
}
}

// Pick first iOS app (in the realm) for Store redirect.
Expand All @@ -47,7 +53,13 @@ func (c *Controller) getAppStoreData(realmID uint) (*AppStoreData, error) {
return nil, fmt.Errorf("failed to get iOS Apps: %w", err)
}
if len(iosApps) > 0 {
iosURL = iosApps[0].URL
// Find the first app that has redirects enabled.
for _, app := range iosApps {
if !app.DisableRedirect {
iosURL = app.URL
break
}
}
}

return &AppStoreData{
Expand Down
19 changes: 11 additions & 8 deletions pkg/controller/redirect/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,33 @@ func isIOS(userAgent string) bool {
}

// decideRedirect selects where to redirect based on several signals.
func decideRedirect(region, userAgent string, url url.URL, appStoreData AppStoreData) (string, bool) {
func decideRedirect(region, userAgent string, url *url.URL, enxEnabled bool, appStoreData AppStoreData) (string, bool) {
// Canonicalize path as lowercase.
path := strings.ToLower(url.Path)

// Check for browser type.
onAndroid := isAndroid(userAgent)
onIOS := isIOS(userAgent)

// On Android redirect to Play Store if App Link doesn't trigger
// and an a link is set up.
if onAndroid && appStoreData.AndroidURL != "" && appStoreData.AndroidAppID != "" {
return buildIntentURL(path, url.Query(), region, appStoreData.AndroidAppID, appStoreData.AndroidURL), true
// On Android redirect to Play Store if App Link doesn't trigger and an a link
// is set up.
if onAndroid && appStoreData.AndroidAppID != "" && appStoreData.AndroidURL != "" {
intent := buildIntentURL(path, url.Query(), region, appStoreData.AndroidAppID, appStoreData.AndroidURL)
return intent, true
}

// On iOS redirect to App Store if App Link doesn't trigger
// and an a link is set up.
// On iOS redirect to App Store if App Link doesn't trigger and an a link is
// set up.
if onIOS && appStoreData.IOSURL != "" {
return appStoreData.IOSURL, true
}

if onIOS || onAndroid {
// If enx is enabled, return that URL.
if enxEnabled && (onAndroid || onIOS) {
return buildEnsURL(path, url.Query(), region), true
}

// The request included no matching metadata, do nothing.
return "", false
}

Expand Down
61 changes: 44 additions & 17 deletions pkg/controller/redirect/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,6 @@ func TestDecideRedirect(t *testing.T) {
AndroidAppID: "gov.moosylvania.app",
IOSURL: "https://ios.example.com/store/moosylvania",
}
appLinkOnlyAndroid := AppStoreData{
AndroidURL: "https://android.example.com/store/moosylvania",
AndroidAppID: "gov.moosylvania.app",
IOSURL: "",
}
appLinkNeither := AppStoreData{
AndroidURL: "",
AndroidAppID: "",
Expand All @@ -288,50 +283,82 @@ func TestDecideRedirect(t *testing.T) {
host string
url string
altURL *url.URL
enxEnabled bool
userAgent string
appStoreData *AppStoreData
expected string
}{
// Android
{
name: "moosylvania_android_both",
name: "android_both",
url: "https://moosylvania.gov/v?c=1234567890abcdef",
userAgent: userAgentAndroid,
appStoreData: &appLinkBoth,
expected: "intent://v?c=1234567890abcdef&r=US-MOO" + expectedSuffix,
},
{
name: "moosylvania_android_both_relative",
name: "android_both_relative",
altURL: &relativePinURL,
userAgent: userAgentAndroid,
appStoreData: &appLinkBoth,
expected: "intent://v?c=1234567890abcdef&r=US-MOO" + expectedSuffix,
},
{
name: "moosylvania_android_no_applink",
name: "android_no_applink_enx",
url: "https://moosylvania.gov/v?c=1234567890abcdef",
enxEnabled: true,
userAgent: userAgentAndroid,
appStoreData: &appLinkNeither,
expected: "ens://v?c=1234567890abcdef&r=US-MOO",
},
{
name: "moosylvania_ios_no_applink",
name: "android_no_applink",
url: "https://moosylvania.gov/v?c=1234567890abcdef",
enxEnabled: false,
userAgent: userAgentAndroid,
appStoreData: &appLinkNeither,
expected: "",
},

// iOS
{
name: "ios_both",
url: "https://ios.example.com/store/moosylvania",
userAgent: userAgentIOS,
appStoreData: &appLinkOnlyAndroid,
expected: "ens://v?c=1234567890abcdef&r=US-MOO",
appStoreData: &appLinkBoth,
expected: "https://ios.example.com/store/moosylvania",
},
{
name: "moosylvania_ios_no_applink",
url: "https://moosylvania.gov/v?c=1234567890abcdef",
name: "ios_both_relative",
url: "https://ios.example.com/store/moosylvania",
altURL: &relativePinURL,
userAgent: userAgentIOS,
appStoreData: &appLinkBoth,
expected: "https://ios.example.com/store/moosylvania",
},
{
name: "moosylvania_windows",
name: "ios_no_applink_enx",
url: "https://moosylvania.gov/v?c=1234567890abcdef",
enxEnabled: true,
userAgent: userAgentIOS,
appStoreData: &appLinkNeither,
expected: "ens://v?c=1234567890abcdef&r=US-MOO",
},
{
name: "ios_no_applink",
url: "https://moosylvania.gov/v?c=1234567890abcdef",
enxEnabled: false,
userAgent: userAgentIOS,
appStoreData: &appLinkNeither,
expected: "",
},

// Other
{
name: "windows",
url: "https://moosylvania.gov/v?c=1234567890abcdef",
userAgent: userAgentNeither,
appStoreData: &appLinkOnlyAndroid,
appStoreData: &appLinkBoth,
expected: "",
},
}
Expand All @@ -349,9 +376,9 @@ func TestDecideRedirect(t *testing.T) {
}
url = otherURL
}
result, success := decideRedirect("US-MOO", tc.userAgent, *url, *tc.appStoreData)
result, success := decideRedirect("US-MOO", tc.userAgent, url, tc.enxEnabled, *tc.appStoreData)
if tc.expected != result {
t.Errorf("expected %s, got %s", tc.expected, result)
t.Errorf("expected %q to be %q", result, tc.expected)
}
if (tc.expected != "") != success {
t.Errorf("expected doesn't match success")
Expand Down
Loading