forked from MastodonKit/MastodonKit
/
Login.swift
48 lines (42 loc) · 1.86 KB
/
Login.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// Login.swift
// MastodonKit
//
// Created by Ornithologist Coder on 4/18/17.
// Copyright © 2017 MastodonKit. All rights reserved.
//
import Foundation
public struct Login {
/// Performs a silent login.
///
/// - Parameters:
/// - clientID: The client ID.
/// - clientSecret: The client secret.
/// - scopes: The access scopes.
/// - username: The user's username or e-mail address.
/// - password: The user's password.
/// - Returns: Request for `LoginSettings`.
public static func silent(clientID: String, clientSecret: String, scopes: [AccessScope], username: String, password: String) -> Request<LoginSettings> {
let parameters = [
Parameter(name: "client_id", value: clientID),
Parameter(name: "client_secret", value: clientSecret),
Parameter(name: "scope", value: scopes.map(toString).joined(separator: " ")),
Parameter(name: "grant_type", value: "password"),
Parameter(name: "username", value: username),
Parameter(name: "password", value: password)
]
let method = HTTPMethod.post(.parameters(parameters))
return Request<LoginSettings>(path: "/oauth/token", method: method)
}
public static func authorize(code: String, clientID: String, clientSecret: String, redirectURI: String) -> Request<LoginSettings> {
let parameters = [
Parameter(name: "client_id", value: clientID),
Parameter(name: "client_secret", value: clientSecret),
Parameter(name: "redirect_uri", value: redirectURI),
Parameter(name: "grant_type", value: "authorization_code"),
Parameter(name: "code", value: code)
]
let method = HTTPMethod.post(.parameters(parameters))
return Request<LoginSettings>(path: "/oauth/token", method: method)
}
}