createQuickstartAPI creates an Auth0 API resource server and optionally links it to an existing application client via a client grant.
(ctx context.Context, cli *cli, inputs SetupInputs)
| 1184 | // createQuickstartAPI creates an Auth0 API resource server and optionally links it to an |
| 1185 | // existing application client via a client grant. |
| 1186 | func createQuickstartAPI(ctx context.Context, cli *cli, inputs SetupInputs) error { |
| 1187 | // API name = "<app-name>-API", fallback to identifier. |
| 1188 | apiName := inputs.Identifier |
| 1189 | if inputs.Name != "" { |
| 1190 | apiName = inputs.Name + "-API" |
| 1191 | } |
| 1192 | |
| 1193 | tokenLifetime, tokenErr := strconv.Atoi(inputs.TokenLifetime) |
| 1194 | if tokenErr != nil || tokenLifetime <= 0 { |
| 1195 | if inputs.TokenLifetime != "" && inputs.TokenLifetime != strconv.Itoa(apiDefaultTokenLifetime) { |
| 1196 | cli.renderer.Warnf("Invalid token lifetime %q, using default %d seconds", inputs.TokenLifetime, apiDefaultTokenLifetime) |
| 1197 | } |
| 1198 | tokenLifetime = apiDefaultTokenLifetime |
| 1199 | } |
| 1200 | |
| 1201 | rs := &management.ResourceServer{ |
| 1202 | Name: &apiName, |
| 1203 | Identifier: &inputs.Identifier, |
| 1204 | SigningAlgorithm: &inputs.SigningAlg, |
| 1205 | TokenLifetime: &tokenLifetime, |
| 1206 | AllowOfflineAccess: &inputs.OfflineAccess, |
| 1207 | } |
| 1208 | |
| 1209 | if inputs.Scopes != "" { |
| 1210 | var scopeList []string |
| 1211 | for _, s := range strings.Split(inputs.Scopes, ",") { |
| 1212 | if s = strings.TrimSpace(s); s != "" { |
| 1213 | scopeList = append(scopeList, s) |
| 1214 | } |
| 1215 | } |
| 1216 | if len(scopeList) > 0 { |
| 1217 | rs.Scopes = apiScopesFor(scopeList) |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | if err := ansi.Waiting(func() error { |
| 1222 | return cli.api.ResourceServer.Create(ctx, rs) |
| 1223 | }); err != nil { |
| 1224 | return fmt.Errorf("failed to create API with name %q and identifier %q: %w", apiName, inputs.Identifier, err) |
| 1225 | } |
| 1226 | printAPIDetails(cli, rs) |
| 1227 | |
| 1228 | // Link the app to the API via a client grant if an app was selected/created. |
| 1229 | if inputs.LinkedAppID != "" { |
| 1230 | emptyScopes := []string{} |
| 1231 | grant := &management.ClientGrant{ |
| 1232 | ClientID: &inputs.LinkedAppID, |
| 1233 | Audience: &inputs.Identifier, |
| 1234 | Scope: &emptyScopes, |
| 1235 | } |
| 1236 | if grantErr := ansi.Waiting(func() error { |
| 1237 | return cli.api.ClientGrant.Create(ctx, grant) |
| 1238 | }); grantErr != nil { |
| 1239 | cli.renderer.Warnf("Failed to link application to API: %v", grantErr) |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | return nil |