MCPcopy Create free account
hub / github.com/SmartThingsCommunity/smartthings-cli / login

Function login

src/lib/login-authenticator.ts:137–244  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

135 }
136
137 const login = async (): Promise<void> => {
138 const app = express()
139 const port = await getPort({ ports: [61973, 61974, 61975] })
140 const finishURL = `http://localhost:${port}/finish`
141 const baseOAuthInURL = clientIdProvider.baseOAuthInURL
142
143 const verifier = randomBytes(32).toString('base64url')
144 const codeChallenge = sha256(verifier).toString('base64url')
145
146 let loginFailed = false
147
148 app.get('/start', (_req, res) => {
149 const authorizeURL = new URL(`${baseOAuthInURL}/authorize`)
150 authorizeURL.search = new URLSearchParams({
151 /* eslint-disable @typescript-eslint/naming-convention */
152 scope: scopes.join('+'),
153 response_type: 'code',
154 client_id: clientId,
155 code_challenge: codeChallenge,
156 code_challenge_method: 'S256',
157 redirect_uri: finishURL,
158 client_type: 'USER_LEVEL',
159 /* eslint-enable @typescript-eslint/naming-convention */
160 }).toString()
161
162 logger.debug('redirecting to', `${authorizeURL.origin}${authorizeURL.pathname}`)
163 res.redirect(authorizeURL.toString())
164 })
165
166 app.get('/finish', (req, res) => {
167 if ('error' in req.query) {
168 logger.error('error trying to authenticate', req.query.error)
169 if ('error_description' in req.query) {
170 logger.error(`${req.query.error_description}`)
171 }
172
173 loginFailed = true
174 res.send('<html><body><h1>Failure trying to authenticate.</h1></body></html>')
175 return
176 }
177
178 const requestBody = {
179 /* eslint-disable @typescript-eslint/naming-convention */
180 'grant_type': 'authorization_code',
181 'client_id': clientId,
182 'code_verifier': verifier,
183 'code': req.query.code,
184 'redirect_uri': finishURL,
185 /* eslint-enable @typescript-eslint/naming-convention */
186 }
187
188 logger.debug(`making axios request: ${baseOAuthInURL}/token`)
189 axios.post(`${baseOAuthInURL}/token`, qs.stringify(requestBody), postConfig)
190 // eslint-disable-next-line @typescript-eslint/no-explicit-any
191 .then((response: AxiosResponse<any>) => {
192 updateTokenFromResponse(response)
193 res.send('<html><body><h1>You can close the window.</h1></body></html>')
194 })

Callers 1

authenticateFunction · 0.85

Calls 4

sha256Function · 0.85
updateTokenFromResponseFunction · 0.85
delayFunction · 0.85
scrubAuthInfoFunction · 0.70

Tested by

no test coverage detected