github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/web/auth/deprecated_app_list.go (about)

     1  package auth
     2  
     3  import (
     4  	"html/template"
     5  	"strings"
     6  
     7  	"github.com/cozy/cozy-stack/model/instance"
     8  	"github.com/cozy/cozy-stack/model/oauth"
     9  	"github.com/cozy/cozy-stack/pkg/config/config"
    10  	"github.com/cozy/cozy-stack/pkg/logger"
    11  	"github.com/cozy/cozy-stack/web/middlewares"
    12  	"github.com/mssola/user_agent"
    13  )
    14  
    15  const DefaultStoreURL = "https://cozy.io/fr/download/"
    16  
    17  // DeprecatedAppList lists and detects the deprecated apps.
    18  type DeprecatedAppList struct {
    19  	apps   []config.DeprecatedApp
    20  	logger *logger.Entry
    21  }
    22  
    23  // NewDeprecatedAppList instantiates a new [DeprecatedAppList].
    24  func NewDeprecatedAppList(cfg config.DeprecatedAppsCfg) *DeprecatedAppList {
    25  	return &DeprecatedAppList{
    26  		apps:   cfg.Apps,
    27  		logger: logger.WithNamespace("deprecated"),
    28  	}
    29  }
    30  
    31  // IsDeprecated returns true if the given client is marked as deprectated.
    32  func (d *DeprecatedAppList) IsDeprecated(client *oauth.Client) bool {
    33  	for _, app := range d.apps {
    34  		if client.SoftwareID == app.SoftwareID {
    35  			return true
    36  		}
    37  	}
    38  
    39  	return false
    40  }
    41  
    42  func (d *DeprecatedAppList) RenderArgs(client *oauth.Client, inst *instance.Instance, uaStr string) map[string]interface{} {
    43  	ua := user_agent.New(uaStr)
    44  
    45  	var app config.DeprecatedApp
    46  
    47  	for _, a := range d.apps {
    48  		if client.SoftwareID == a.SoftwareID {
    49  			app = a
    50  			break
    51  		}
    52  	}
    53  
    54  	platform := strings.ToLower(ua.Platform())
    55  
    56  	if strings.Contains(strings.ToLower(ua.OS()), "android") ||
    57  		strings.Contains(strings.ToLower(uaStr), "android") {
    58  		platform = "android"
    59  	}
    60  
    61  	if strings.Contains(strings.ToLower(ua.OS()), "iphone") ||
    62  		strings.Contains(strings.ToLower(uaStr), "iphone") ||
    63  		platform == "ipad" {
    64  		platform = "iphone"
    65  	}
    66  
    67  	if platform != "iphone" && platform != "android" {
    68  		platform = "other"
    69  	}
    70  
    71  	storeURL := DefaultStoreURL
    72  	if url, ok := app.StoreURLs[platform]; ok {
    73  		storeURL = url
    74  	}
    75  
    76  	d.logger.WithDomain(inst.Domain).
    77  		WithField("platform", platform).
    78  		WithField("app", app.Name).
    79  		Info("Deprecated app detected, stop authentication")
    80  
    81  	res := map[string]interface{}{
    82  		"Domain":      inst.ContextualDomain(),
    83  		"ContextName": inst.ContextName,
    84  		"Locale":      inst.Locale,
    85  		"Title":       inst.TemplateTitle(),
    86  		"Favicon":     middlewares.Favicon(inst),
    87  		"AppName":     app.Name,
    88  		"Platform":    platform,
    89  		// template.URL is used in order to avoid the discard of url starting
    90  		// by `market://` (the url is replaced by "#ZgotmplZ" in case of discard).
    91  		//
    92  		// More details at https://github.com/golang/go/blob/bce7aec3cdca8580585095007e9b7cea11a8812f/src/html/template/url.go#L19
    93  		"StoreURL": template.URL(storeURL),
    94  	}
    95  
    96  	return res
    97  }