NUGU SDK Linux  1.7.5
nugu_auth.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 SK Telecom Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __NUGU_AUTH_H__
18 #define __NUGU_AUTH_H__
19 
21 #include <string>
22 #include <vector>
23 
24 namespace NuguClientKit {
25 
40 enum GrantType {
41  AUTHORIZATION_CODE = 0x1, /* OAuth2 Authorization code */
42  CLIENT_CREDENTIALS = 0x2, /* OAuth2 Client credentials */
43  REFRESH_TOKEN = 0x4, /* OAuth2 refresh token */
44  DEVICE_CODE = 0x8 /* OAuth2 device code */
45 };
46 
51  std::string oauth_server_url; /* OAuth2 server url */
52  std::string oauth_client_id; /* Client id */
53  std::string oauth_client_secret; /* Client secret */
54  std::string oauth_redirect_uri; /* Redirect uri */
55  std::string device_type_code; /* Device type code */
56  std::string poc_id; /* PoC id */
57 };
58 
62 struct NuguToken {
63  std::string access_token; /* OAuth2 access token */
64  std::string refresh_token; /* OAuth2 refresh token */
65  std::string token_type; /* OAuth2 token type. e.g. "Bearer" */
66  int expires_in; /* Expiration time remaining after token issuance (secs) */
67  time_t timestamp; /* Token issuance time */
68 
69  std::vector<std::string> scope; /* scope */
70  int exp; /* Token expiration time (unix timestamp) */
71  std::string client_id; /* OAuth2 client id */
72  std::string ext_usr; /* user id */
73  std::string ext_dvc; /* device id */
74  std::string ext_srl; /* device serial */
75  std::string ext_poc; /* PoC id */
76 
77  bool sid; /* flag for server-initiative-directive support */
78 };
79 
83 struct AuthResponse {
84  int code; /* HTTP Response code */
85  std::string body; /* HTTP Response body */
86 };
87 
91 class NuguAuth {
92 public:
93  explicit NuguAuth(const NuguDeviceConfig& config);
94  ~NuguAuth();
95 
103  bool discovery(struct AuthResponse* response = nullptr);
104 
112  bool discovery(const std::function<void(bool success, const struct AuthResponse* response)>& cb);
113 
121  bool isSupport(const GrantType& gtype);
122 
128  std::string generateAuthorizeUrl(const std::string& device_serial);
129 
137  NuguToken* getAuthorizationCodeToken(const std::string& code, const std::string& device_serial,
138  struct AuthResponse* response = nullptr);
139 
146  NuguToken* getClientCredentialsToken(const std::string& device_serial,
147  struct AuthResponse* response = nullptr);
148 
156 
165  bool refresh(NuguToken* token, const std::string& device_serial = "",
166  struct AuthResponse* response = nullptr);
167 
175  bool isExpired(const NuguToken* token, time_t base_time = 0);
176 
183  time_t getRemainExpireTime(const NuguToken* token, time_t base_time = 0);
184 
189  std::string getGatewayRegistryUri();
190 
195  std::string getTemplateServerUri();
196 
197 private:
198  bool parseDiscoveryResult(const std::string& response);
199  bool parseAndSaveToken(const std::string& response, NuguToken* token);
200 
201  NuguDeviceConfig config;
202  NuguHttpRest* rest;
203  unsigned int supported_grant_types;
204  std::string ep_token;
205  std::string ep_authorization;
206  std::string uri_gateway_registry;
207  std::string uri_template_server;
208 };
209 
214 } // NuguClientKit
215 
216 #endif /* __NUGU_AUTH_H__ */
NuguAuth.
Definition: nugu_auth.hh:91
time_t getRemainExpireTime(const NuguToken *token, time_t base_time=0)
Get remaining expiration time based on base_time.
bool parseAccessToken(NuguToken *token)
Parsing the JWT access_token and fill the token information.
bool isExpired(const NuguToken *token, time_t base_time=0)
Check the token is expired or not.
bool isSupport(const GrantType &gtype)
Check whether the requested grant type is supported for the client.
bool discovery(const std::function< void(bool success, const struct AuthResponse *response)> &cb)
Async OAuth2 discovery to get OAuth2 end-point and server url.
bool discovery(struct AuthResponse *response=nullptr)
OAuth2 discovery to get OAuth2 end-point and server url.
NuguToken * getAuthorizationCodeToken(const std::string &code, const std::string &device_serial, struct AuthResponse *response=nullptr)
Get the token using authorization code (token exchange)
bool refresh(NuguToken *token, const std::string &device_serial="", struct AuthResponse *response=nullptr)
Refresh the access_token and update the token information.
std::string generateAuthorizeUrl(const std::string &device_serial)
Get OAuth2 authorization url.
NuguToken * getClientCredentialsToken(const std::string &device_serial, struct AuthResponse *response=nullptr)
Get the token using client credentials.
std::string getTemplateServerUri()
Get uri for template server.
std::string getGatewayRegistryUri()
Get uri for device gateway registry.
NUGU HTTP Rest.
Definition: nugu_http_rest.hh:41
GrantType
GrantType.
Definition: nugu_auth.hh:40
AuthResponse.
Definition: nugu_auth.hh:83
NuguDeviceConfig.
Definition: nugu_auth.hh:50
NuguToken.
Definition: nugu_auth.hh:62