Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 committed Oct 28, 2021
1 parent 756954a commit b570bd9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 66 deletions.
70 changes: 16 additions & 54 deletions pkg/storage/registry/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/cs3org/reva/pkg/storage/registry/registry"
"github.com/cs3org/reva/pkg/storage/utils/templates"
"github.com/cs3org/reva/pkg/useragent"
ua "github.com/mileusna/useragent"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -113,23 +112,7 @@ func getProviderAddr(ctx context.Context, r rule) string {

func (b *reg) ListProviders(ctx context.Context) ([]*registrypb.ProviderInfo, error) {
providers := []*registrypb.ProviderInfo{}
for k, v := range b.c.Rules {

// check if the provider is allowed to be shown according to the
// user agent that made the request
// if the list of AllowedUserAgents is empty, this means that
// every agents that made the request could see the provider

if len(v.AllowedUserAgents) != 0 {
ua, ok := ctxpkg.ContextGetUserAgent(ctx)
if !ok {
continue
}
if !userAgentIsAllowed(ua, v.AllowedUserAgents) {
continue // skip this provider
}
}

for k, v := range rulesFilteredByUserAgent(ctx, b.c.Rules) {
if addr := getProviderAddr(ctx, v); addr != "" {
combs := generateRegexCombinations(k)
for _, c := range combs {
Expand Down Expand Up @@ -157,28 +140,22 @@ func (b *reg) GetHome(ctx context.Context) (*registrypb.ProviderInfo, error) {
return nil, errors.New("static: home not found")
}

func userAgentIsAllowed(ua *ua.UserAgent, userAgents []string) bool {
for _, userAgent := range userAgents {
switch userAgent {
case "web":
if useragent.IsWeb(ua) {
return true
}
case "mobile":
if useragent.IsMobile(ua) {
return true
}
case "desktop":
if useragent.IsDesktop(ua) {
return true
}
case "grpc":
if useragent.IsGRPC(ua) {
return true
func rulesFilteredByUserAgent(ctx context.Context, rules map[string]rule) map[string]rule {
filtered := make(map[string]rule)
for prefix, rule := range rules {
// check if the provider is allowed to be shown according to the
// user agent that made the request
// if the list of AllowedUserAgents is empty, this means that
// every agents that made the request could see the provider

if len(rule.AllowedUserAgents) != 0 {
if ua, ok := ctxpkg.ContextGetUserAgent(ctx); !ok || !useragent.IsAllowed(ua, rule.AllowedUserAgents) {
continue
}
}
filtered[prefix] = rule
}
return false
return filtered
}

func (b *reg) FindProviders(ctx context.Context, ref *provider.Reference) ([]*registrypb.ProviderInfo, error) {
Expand All @@ -189,7 +166,7 @@ func (b *reg) FindProviders(ctx context.Context, ref *provider.Reference) ([]*re
// If the reference has a resource id set, use it to route
if ref.ResourceId != nil {
if ref.ResourceId.StorageId != "" {
for prefix, rule := range b.c.Rules {
for prefix, rule := range rulesFilteredByUserAgent(ctx, b.c.Rules) {
addr := getProviderAddr(ctx, rule)
r, err := regexp.Compile("^" + prefix + "$")
if err != nil {
Expand All @@ -215,22 +192,7 @@ func (b *reg) FindProviders(ctx context.Context, ref *provider.Reference) ([]*re
// TODO this needs to be reevaluated once all clients query the storage registry for a list of storage providers
fn := path.Clean(ref.GetPath())
if fn != "" {
for prefix, rule := range b.c.Rules {

// check if the provider is allowed to be shown according to the
// user agent that made the request
// if the list of AllowedUserAgents is empty, this means that
// every agents that made the request could see the provider

if len(rule.AllowedUserAgents) != 0 {
ua, ok := ctxpkg.ContextGetUserAgent(ctx)
if !ok {
continue
}
if !userAgentIsAllowed(ua, rule.AllowedUserAgents) {
continue // skip this provider
}
}
for prefix, rule := range rulesFilteredByUserAgent(ctx, b.c.Rules) {

addr := getProviderAddr(ctx, rule)
r, err := regexp.Compile("^" + prefix)
Expand Down
50 changes: 40 additions & 10 deletions pkg/useragent/useragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,55 @@ import (
ua "github.com/mileusna/useragent"
)

// IsWeb returns true if the useragent is generated by the web
func IsWeb(ua *ua.UserAgent) bool {
// isWeb returns true if the useragent is generated by the web
func isWeb(ua *ua.UserAgent) bool {
return ua.IsChrome() || ua.IsEdge() || ua.IsFirefox() || ua.IsSafari() ||
ua.IsInternetExplorer() || ua.IsOpera() || ua.IsOperaMini()
}

// IsWeb returns true if the useragent is generated by the mobile
func IsMobile(ua *ua.UserAgent) bool {
// isMobile returns true if the useragent is generated by the mobile
func isMobile(ua *ua.UserAgent) bool {
// workaround as the library does not recognise iOS string inside the user agent
isIOS := ua.IsIOS() || strings.Contains(ua.String, "iOS")
return !IsWeb(ua) && (ua.IsAndroid() || isIOS)
return !isWeb(ua) && (ua.IsAndroid() || isIOS)
}

// IsWeb returns true if the useragent is generated by a desktop application
func IsDesktop(ua *ua.UserAgent) bool {
return ua.Desktop && !IsWeb(ua)
// isDesktop returns true if the useragent is generated by a desktop application
func isDesktop(ua *ua.UserAgent) bool {
return ua.Desktop && !isWeb(ua)
}

// IsWeb returns true if the useragent is generated by a grpc client
func IsGRPC(ua *ua.UserAgent) bool {
// isGRPC returns true if the useragent is generated by a grpc client
func isGRPC(ua *ua.UserAgent) bool {
return strings.HasPrefix(ua.Name, "grpc")
}

// IsAllowed returns true if the user agent category is one in the user agents list
func IsAllowed(ua *ua.UserAgent, userAgents []string) bool {
isWeb := isWeb(ua)
isMobile := isMobile(ua)
isDesktop := isDesktop(ua)
isGRPC := isGRPC(ua)

for _, userAgent := range userAgents {
switch userAgent {
case "web":
if isWeb {
return true
}
case "mobile":
if isMobile {
return true
}
case "desktop":
if isDesktop {
return true
}
case "grpc":
if isGRPC {
return true
}
}
}
return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package static
package useragent

import (
"testing"
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestUserAgentIsAllowed(t *testing.T) {

ua := ua.Parse(tt.userAgent)

res := userAgentIsAllowed(&ua, tt.userAgents)
res := IsAllowed(&ua, tt.userAgents)

if res != tt.expected {
t.Fatalf("result does not match with expected. got=%+v expected=%+v", res, tt.expected)
Expand Down

0 comments on commit b570bd9

Please sign in to comment.