NUGU SDK Linux  1.7.6
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 
20 #include <string>
21 #include <vector>
22 
24 #include <nugu.h>
25 
26 namespace NuguClientKit {
27 
42 enum GrantType {
43  AUTHORIZATION_CODE = 0x1, /* OAuth2 Authorization code */
44  CLIENT_CREDENTIALS = 0x2, /* OAuth2 Client credentials */
45  REFRESH_TOKEN = 0x4, /* OAuth2 refresh token */
46  DEVICE_CODE = 0x8 /* OAuth2 device code */
47 };
48 
53  std::string oauth_server_url; /* OAuth2 server url */
54  std::string oauth_client_id; /* Client id */
55  std::string oauth_client_secret; /* Client secret */
56  std::string oauth_redirect_uri; /* Redirect uri */
57  std::string device_type_code; /* Device type code */
58  std::string poc_id; /* PoC id */
59 };
60 
64 struct NuguToken {
65  std::string access_token; /* OAuth2 access token */
66  std::string refresh_token; /* OAuth2 refresh token */
67  std::string token_type; /* OAuth2 token type. e.g. "Bearer" */
68  int expires_in; /* Expiration time remaining after token issuance (secs) */
69  time_t timestamp; /* Token issuance time */
70 
71  std::vector<std::string> scope; /* scope */
72  int exp; /* Token expiration time (unix timestamp) */
73  std::string client_id; /* OAuth2 client id */
74  std::string ext_usr; /* user id */
75  std::string ext_dvc; /* device id */
76  std::string ext_srl; /* device serial */
77  std::string ext_poc; /* PoC id */
78 
79  bool sid; /* flag for server-initiative-directive support */
80 };
81 
85 struct AuthResponse {
86  int code; /* HTTP Response code */
87  std::string body; /* HTTP Response body */
88 };
89 
93 class NUGU_API NuguAuth {
94 public:
95  explicit NuguAuth(const NuguDeviceConfig& config);
96  ~NuguAuth();
97 
105  bool discovery(struct AuthResponse* response = nullptr);
106 
114  bool discovery(const std::function<void(bool success, const struct AuthResponse* response)>& cb);
115 
123  bool isSupport(const GrantType& gtype);
124 
130  std::string generateAuthorizeUrl(const std::string& device_serial);
131 
139  NuguToken* getAuthorizationCodeToken(const std::string& code, const std::string& device_serial,
140  struct AuthResponse* response = nullptr);
141 
148  NuguToken* getClientCredentialsToken(const std::string& device_serial,
149  struct AuthResponse* response = nullptr);
150 
158 
167  bool refresh(NuguToken* token, const std::string& device_serial = "",
168  struct AuthResponse* response = nullptr);
169 
177  bool isExpired(const NuguToken* token, time_t base_time = 0);
178 
185  time_t getRemainExpireTime(const NuguToken* token, time_t base_time = 0);
186 
191  std::string getGatewayRegistryUri();
192 
197  std::string getTemplateServerUri();
198 
199 private:
200  bool parseDiscoveryResult(const std::string& response);
201  bool parseAndSaveToken(const std::string& response, NuguToken* token);
202 
203  NuguDeviceConfig config;
204  NuguHttpRest* rest;
205  unsigned int supported_grant_types;
206  std::string ep_token;
207  std::string ep_authorization;
208  std::string uri_gateway_registry;
209  std::string uri_template_server;
210 };
211 
216 } // NuguClientKit
217 
218 #endif /* __NUGU_AUTH_H__ */
NuguAuth.
Definition: nugu_auth.hh:93
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:42
GrantType
GrantType.
Definition: nugu_auth.hh:42
AuthResponse.
Definition: nugu_auth.hh:85
NuguDeviceConfig.
Definition: nugu_auth.hh:52
NuguToken.
Definition: nugu_auth.hh:64