Class: Gitlab::Auth::ExternalUsernameSanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/auth/external_username_sanitizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(external_username) ⇒ ExternalUsernameSanitizer

Returns a new instance of ExternalUsernameSanitizer.



8
9
10
# File 'lib/gitlab/auth/external_username_sanitizer.rb', line 8

def initialize(external_username)
  @external_username = external_username
end

Instance Attribute Details

#external_usernameObject (readonly)

Returns the value of attribute external_username.



6
7
8
# File 'lib/gitlab/auth/external_username_sanitizer.rb', line 6

def external_username
  @external_username
end

Instance Method Details

#sanitizeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/auth/external_username_sanitizer.rb', line 12

def sanitize
  # remove most characters illegal in usernames / slugs
  slug = Gitlab::Slug::Path.new(external_username).generate

  # remove leading - , _ , or . characters not removed by Namespace.clean_path
  slug = slug.sub(/\A[_.-]+/, '')

  # remove trailing - , _ or . characters not removed by Namespace.clean_path
  # hard to write a regex to match end-of-string without ReDoS, so just use plain Ruby
  slug = slug[0...-1] while slug.end_with?('.', '-', '_')

  # remove consecutive - , _ , or . characters
  slug = slug.gsub(/([_.-])[_.-]+/, '\1')

  slug = unique_by_namespace(slug)

  validated_path(slug)
end

#unique_by_namespace(slug) ⇒ Object

decomposed from Namespace.clean_path



32
33
34
35
36
37
# File 'lib/gitlab/auth/external_username_sanitizer.rb', line 32

def unique_by_namespace(slug)
  path = Namespaces::RandomizedSuffixPath.new(slug).to_s
  Gitlab::Utils::Uniquify.new.string(path) do |s|
    Namespace.all.find_by_path_or_name(s)
  end
end

#validated_path(path) ⇒ Object



39
40
41
42
43
# File 'lib/gitlab/auth/external_username_sanitizer.rb', line 39

def validated_path(path)
  Gitlab::Utils::Uniquify.new.string(path) do |s|
    !NamespacePathValidator.valid_path?(s)
  end
end