1
+ /////////////////////////////////////////////////////////////////////
2
+ // Copyright (c) Autodesk, Inc. All rights reserved
3
+ // Written by Forge Partner Development
4
+ //
5
+ // Permission to use, copy, modify, and distribute this software in
6
+ // object code form for any purpose and without fee is hereby granted,
7
+ // provided that the above copyright notice appears in all copies and
8
+ // that both that copyright notice and the limited warranty and
9
+ // restricted rights notice below appear in all supporting
10
+ // documentation.
11
+ //
12
+ // AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
13
+ // AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
14
+ // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
15
+ // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
16
+ // UNINTERRUPTED OR ERROR FREE.
17
+ /////////////////////////////////////////////////////////////////////
18
+
19
+ const { AuthClientThreeLegged } = require ( 'forge-apis' ) ;
20
+
21
+ const config = {
22
+ scopes : {
23
+ // Required scopes for the server-side application
24
+ internal : [ 'bucket:create' , 'bucket:read' , 'data:read' , 'data:create' , 'data:write' ] ,
25
+ // Required scope for the client-side viewer
26
+ public : [ 'viewables:read' ]
27
+ }
28
+ } ;
29
+
30
+ class ThreeOAuth {
31
+ constructor ( session ) {
32
+ this . _session = session ;
33
+ }
34
+
35
+ getClient ( scopes = config . scopes . internal ) {
36
+ return new AuthClientThreeLegged ( process . env . FORGE_CLIENT_ID , process . env . FORGE_CLIENT_SECRET , process . env . FORGE_CALLBACK_URL , scopes ) ;
37
+ }
38
+
39
+ isAuthorized ( ) {
40
+ return ! ! this . _session . public_token ;
41
+ }
42
+
43
+ async getPublicToken ( ) {
44
+ if ( this . _isExpired ( ) ) {
45
+ await this . _refreshTokens ( ) ;
46
+ }
47
+
48
+ return {
49
+ access_token : this . _session . public_token ,
50
+ expires_in : this . _expiresIn ( )
51
+ } ;
52
+ }
53
+
54
+ async getInternalToken ( ) {
55
+ if ( this . _isExpired ( ) ) {
56
+ await this . _refreshTokens ( ) ;
57
+ }
58
+
59
+ return {
60
+ access_token : this . _session . internal_token ,
61
+ expires_in : this . _expiresIn ( )
62
+ } ;
63
+ }
64
+
65
+ // On callback, pass the CODE to this function, it will
66
+ // get the internal and public tokens and store them
67
+ // on the session
68
+ async setCode ( code ) {
69
+ const internalTokenClient = this . getClient ( config . scopes . internal ) ;
70
+ const publicTokenClient = this . getClient ( config . scopes . public ) ;
71
+ const internalCredentials = await internalTokenClient . getToken ( code ) ;
72
+ const publicCredentials = await publicTokenClient . refreshToken ( internalCredentials ) ;
73
+
74
+ const now = new Date ( ) ;
75
+ this . _session . internal_token = internalCredentials . access_token ;
76
+ this . _session . public_token = publicCredentials . access_token ;
77
+ this . _session . refresh_token = publicCredentials . refresh_token ;
78
+ this . _session . expires_at = ( now . setSeconds ( now . getSeconds ( ) + publicCredentials . expires_in ) ) ;
79
+ }
80
+
81
+ _expiresIn ( ) {
82
+ const now = new Date ( ) ;
83
+ const expiresAt = new Date ( this . _session . expires_at )
84
+ return Math . round ( ( expiresAt . getTime ( ) - now . getTime ( ) ) / 1000 ) ;
85
+ } ;
86
+
87
+ _isExpired ( ) {
88
+ return ( new Date ( ) > new Date ( this . _session . expires_at ) ) ;
89
+ }
90
+
91
+ async _refreshTokens ( ) {
92
+ let internalTokenClient = this . getClient ( config . scopes . internal ) ;
93
+ let publicTokenClient = this . getClient ( config . scopes . public ) ;
94
+ const internalCredentials = await internalTokenClient . refreshToken ( { refresh_token : this . _session . refresh_token } ) ;
95
+ const publicCredentials = await publicTokenClient . refreshToken ( internalCredentials ) ;
96
+
97
+ const now = new Date ( ) ;
98
+ this . _session . internal_token = internalCredentials . access_token ;
99
+ this . _session . public_token = publicCredentials . access_token ;
100
+ this . _session . refresh_token = publicCredentials . refresh_token ;
101
+ this . _session . expires_at = ( now . setSeconds ( now . getSeconds ( ) + publicCredentials . expires_in ) ) ;
102
+ }
103
+ }
104
+
105
+ module . exports = ThreeOAuth ;
0 commit comments