Class: Starship::AuthHelper

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/starship/auth_helper.rb

Overview

AuthHelper handles authentication with Apple’s developer portal

Constant Summary collapse

AUTH_ENDPOINT =
"https://idmsa.apple.com/appleauth/auth"
WIDGET_KEY_URL =
"https://appstoreconnect.apple.com/olympus/v1/app/config?hostname=itunesconnect.apple.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #logger, logger_for

Constructor Details

#initializeAuthHelper

Returns a new instance of AuthHelper.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/starship/auth_helper.rb', line 34

def initialize()
  # Create session directory if it doesn't exist
  @session_directory = File.expand_path("~/.starship")
  FileUtils.mkdir_p(@session_directory)

  @widget_key = nil
  @csrf = nil
  @csrf_ts = nil
  @email = nil
  @session_data = {}

  # Initialize Faraday with cookie jar
  @session = Faraday.new do |builder|
    builder.use :cookie_jar, jar: HTTP::CookieJar.new
    builder.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#csrfObject (readonly)

Returns the value of attribute csrf.



24
25
26
# File 'lib/starship/auth_helper.rb', line 24

def csrf
  @csrf
end

#csrf_tsObject (readonly)

Returns the value of attribute csrf_ts.



24
25
26
# File 'lib/starship/auth_helper.rb', line 24

def csrf_ts
  @csrf_ts
end

#sessionObject (readonly)

Returns the value of attribute session.



24
25
26
# File 'lib/starship/auth_helper.rb', line 24

def session
  @session
end

#session_dataObject (readonly)

Returns the value of attribute session_data.



24
25
26
# File 'lib/starship/auth_helper.rb', line 24

def session_data
  @session_data
end

Instance Method Details

#request(endpoint, method: :get, params: nil, body: nil, headers: nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/starship/auth_helper.rb', line 143

def request(endpoint, method: :get, params: nil, body: nil, headers: nil)
  default_headers = {
    "Accept" => "application/json, text/plain, */*",
    "X-Requested-With" => "XMLHttpRequest",
    "X-HTTP-Method-Override" => "GET",
    "csrf" => @csrf,
    "csrf_ts" => @csrf_ts,
  }

  if headers
    default_headers = default_headers.merge(headers)
  end

  response = case method
    when :get
      @session.get(endpoint, params, default_headers)
    when :post
      @session.post(endpoint, body, default_headers)
    when :put
      @session.put(endpoint, body, default_headers)
    when :delete
      @session.delete(endpoint, default_headers)
    when :patch
      @session.patch(endpoint, body, default_headers)
    end

  if response.status == 401 || response.status == 403
    logger.warn "Session invalid or expired. Starting authentication from scratch..."

    self.

    response = self.request(endpoint, method: method, params: params, body: body, headers: headers)
  end

  return response
end

#sign_inBoolean

Sign in to Apple Developer Portal

Returns:

  • (Boolean)

    Whether authentication was successful



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/starship/auth_helper.rb', line 54

def 
  email = ENV["APPLE_DEVELOPER_USERNAME"]
  password = ENV["APPLE_DEVELOPER_PASSWORD"]

  if !email || !password
    raise Error, "Email and password are required. Set APPLE_ID and APPLE_PASSWORD environment variables."
  end

  @email = email
  @client_id = generate_session_id(email)
  cookie_path, session_path = get_paths(email)

  # Try to load existing session
  if File.exist?(session_path)
    load_session
    if validate_token
      return true
    end
  end

  logger.warn "Session invalid or expired. Starting authentication from scratch..."
  @session_data = { "client_id" => @client_id, "email" => email }

  # Start authentication process
  auth_result = authenticate_with_srp(email, password)

  if auth_result == :two_factor_required
    handle_two_factor_auth
  elsif auth_result
    # After successful authentication, get CSRF tokens
    response = @session.get("https://developer.apple.com/account")
    if response.status == 200
      extract_csrf_tokens(response)

      if @csrf && @csrf_ts
        save_session
        return true
      end
    end
  end

  return auth_result
end

#two_factor_provider=(provider) ⇒ Object



29
30
31
32
# File 'lib/starship/auth_helper.rb', line 29

def two_factor_provider=(provider)
  @two_factor_provider = provider
  logger.info "Two-factor provider set to #{provider.class.name}"
end

#validate_tokenBoolean

Check if the current session is valid

Returns:

  • (Boolean)

    Whether the session is valid



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/starship/auth_helper.rb', line 100

def validate_token
  return false unless @session_data["session_id"] && @session_data["scnt"]

  begin
    headers = {
      "Accept" => "application/json, text/plain, */*",
      "Content-Type" => "application/vnd.api+json",
      "X-Requested-With" => "XMLHttpRequest",
      "X-Apple-ID-Session-Id" => @session_data["session_id"],
      "scnt" => @session_data["scnt"],
    }

    response = @session.get(
      "https://developer.apple.com/services-account/v1/certificates",
      nil,
      headers
    )

    if response.status == 403
      # Fetch CSRF tokens after confirming session is valid
      csrf_response = @session.get("https://developer.apple.com/account/resources/certificates/list")

      if csrf_response.status == 200
        extract_csrf_tokens(csrf_response)

        if @csrf && @csrf_ts
          return true
        else
          logger.error "Failed to retrieve CSRF tokens after validating session."
          return false
        end
      end
      return true
    else
      logger.warn "Session is invalid. Will reauthenticate."
      return false
    end
  rescue => e
    logger.error "Authentication status check failed: #{e.message}"
    return false
  end
end