Skip to content

Commit

Permalink
Merge pull request #167 from itsMeDavidV/master
Browse files Browse the repository at this point in the history
Added customizable scopes
  • Loading branch information
jonasman authored Mar 7, 2024
2 parents 028e9ec + 7157d5f commit b3256bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
11 changes: 6 additions & 5 deletions Sources/TeslaSwift/Model/Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,35 +175,36 @@ extension TeslaAPI {
var region: Region? {
switch self {
case .ownerAPI: return nil
case let .fleetAPI(region: region, clientID: _, clientSecret: _, redirectURI: _): return region
case let .fleetAPI(region: region, clientID: _, clientSecret: _, redirectURI: _, scopes: _): return region
}
}

var clientID: String {
switch self {
case .ownerAPI: return "ownerapi"
case let .fleetAPI(region: _, clientID: clientID, clientSecret: _, redirectURI: _): return clientID
case let .fleetAPI(region: _, clientID: clientID, clientSecret: _, redirectURI: _, scopes: _): return clientID
}
}

var clientSecret: String {
switch self {
case .ownerAPI: return "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3"
case let .fleetAPI(region: _, clientID: _, clientSecret: clientSecret, redirectURI: _): return clientSecret
case let .fleetAPI(region: _, clientID: _, clientSecret: clientSecret, redirectURI: _, scopes: _): return clientSecret
}
}

var redirectURI: String {
switch self {
case .ownerAPI: return "https://auth.tesla.com/void/callback"
case let .fleetAPI(region: _, clientID: _, clientSecret: _, redirectURI: redirectURI): return redirectURI
case let .fleetAPI(region: _, clientID: _, clientSecret: _, redirectURI: redirectURI, scopes: _): return redirectURI
}
}

var scope: String {
switch self {
case .ownerAPI: return "openid email offline_access"
case .fleetAPI: return "openid user_data vehicle_device_data offline_access vehicle_cmds vehicle_charging_cmds energy_device_data energy_cmds"
case let .fleetAPI(region: _, clientID: _, clientSecret: _, redirectURI: _, scopes: scopes):
return scopes.map { $0.rawValue }.joined(separator: " ")
}
}
}
15 changes: 13 additions & 2 deletions Sources/TeslaSwift/TeslaSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,25 @@ public enum TeslaAPI {
case europeMiddleEastAfrica = "https://fleet-api.prd.eu.vn.cloud.tesla.com"
case china = "https://fleet-api.prd.cn.vn.cloud.tesla.cn"
}

public enum Scope: String {
case openId = "openid"
case offlineAccess = "offline_access"
case userData = "user_data"
case vehicleDeviceData = "vehicle_device_data"
case vehicleCmds = "vehicle_cmds"
case vehicleChargingCmds = "vehicle_charging_cmds"
case energyDeviceData = "energy_device_data"
case energyCmds = "energy_cmds"
}

case ownerAPI
case fleetAPI(region: Region, clientID: String, clientSecret: String, redirectURI: String)
case fleetAPI(region: Region, clientID: String, clientSecret: String, redirectURI: String, scopes: [Scope])

var url: String {
switch self {
case .ownerAPI: return "https://owner-api.teslamotors.com"
case let .fleetAPI(region: region, clientID: _, clientSecret: _, redirectURI: _): return region.rawValue
case let .fleetAPI(region: region, clientID: _, clientSecret: _, redirectURI: _, scopes: _): return region.rawValue
}
}
}
Expand Down

0 comments on commit b3256bd

Please sign in to comment.