Skip to content

Commit

Permalink
Merge pull request #5 from gofynd/fyndx1
Browse files Browse the repository at this point in the history
Latest changes from x1
  • Loading branch information
aparnab93 authored Mar 30, 2022
2 parents fdb441c + 77227bb commit 8a72ce1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<json.version>20211205</json.version>
<jedis.version>2.8.1</jedis.version>
<commons.version>1.5.0</commons.version>
<fdk-client.version>v0.0.16-SNAPSHOT</fdk-client.version>
<fdk-client.version>v0.0.4</fdk-client.version>
<logging.version>3.9.0</logging.version>
<okhttp.version>3.0.0-RC1</okhttp.version>
</properties>
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/com/fynd/extension/config/BaseMVCConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@
public class BaseMVCConfigurer implements WebMvcConfigurer {

@Autowired
private SessionInterceptor sessionInterceptor;
SessionInterceptor sessionInterceptor;

@Autowired
private PlatformInterceptor platformInterceptor;
PlatformInterceptor platformInterceptor;

@Autowired
private ApplicationInterceptor applicationInterceptor;
ApplicationInterceptor applicationInterceptor;

@Override
public void addInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(sessionInterceptor).addPathPatterns("/fp/auth","/platform/**").order(Ordered.HIGHEST_PRECEDENCE);
registry.addInterceptor(platformInterceptor).addPathPatterns("/platform/**").order(Ordered.LOWEST_PRECEDENCE);
registry.addInterceptor(applicationInterceptor).addPathPatterns("/application/**");
registry.addInterceptor(sessionInterceptor)
.addPathPatterns("/fp/auth", "/platform/**")
.order(Ordered.HIGHEST_PRECEDENCE);
registry.addInterceptor(platformInterceptor)
.addPathPatterns("/platform/**")
.order(Ordered.LOWEST_PRECEDENCE);
registry.addInterceptor(applicationInterceptor)
.addPathPatterns("/application/**");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
import com.sdk.platform.PlatformConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -49,8 +46,7 @@ public class ExtensionController {
@GetMapping(path = "/install")
public ResponseEntity<?> install(@RequestParam(value = "company_id") String companyId,
@RequestParam(value = "application_id", required = false) String applicationId,
HttpServletResponse response,
HttpServletRequest request) {
HttpServletResponse response, HttpServletRequest request) {

try {
if (StringUtils.isEmpty(companyId)) {
Expand All @@ -64,15 +60,13 @@ public ResponseEntity<?> install(@RequestParam(value = "company_id") String comp
if (ext.isOnlineAccessMode()) {
session = new Session(Session.generateSessionId(true, null), true);
} else {
sid = Session.generateSessionId(false, new Option(companyId,
ext.getExtensionProperties()
.getCluster()));
sid = Session.generateSessionId(false, new Option(companyId, ext.getExtensionProperties()
.getCluster()));
session = sessionStorage.getSession(sid);
if (ObjectUtils.isEmpty(session)) {
session = new Session(sid, true);
} else if (!Objects.equals(session.getExtension_id(),
ext.getExtensionProperties()
.getApi_key())) {
} else if (!Objects.equals(session.getExtension_id(), ext.getExtensionProperties()
.getApi_key())) {
session = new Session(sid, true);
}
}
Expand Down Expand Up @@ -122,13 +116,13 @@ public ResponseEntity<?> install(@RequestParam(value = "company_id") String comp
// start authorization flow
String redirectUrl = platformConfig.getPlatformOauthClient()
.getAuthorizationURL(session.getScope(), authCallback,
session.getState(),
ext.isOnlineAccessMode());
session.getState(), ext.isOnlineAccessMode());
sessionStorage.saveSession(session);
return ResponseEntity.status(HttpStatus.PERMANENT_REDIRECT)
return ResponseEntity.status(HttpStatus.TEMPORARY_REDIRECT)
.header(Fields.X_COMPANY_ID, companyId)
.header(HttpHeaders.LOCATION, redirectUrl)
.header(HttpHeaders.SET_COOKIE, resCookie.toString())
// .cacheControl(CacheControl.noCache())
.build();
} catch (Exception error) {
log.error("Exception in install call ", error);
Expand All @@ -143,13 +137,11 @@ public ResponseEntity<?> authorize(@RequestParam(value = "company_id") String co
@RequestParam(value = "code", required = false) String code,
@RequestParam(value = "state") String state,
@RequestParam(value = "application_id", required = false) String applicationId,
HttpServletRequest request,
HttpServletResponse response) {
HttpServletRequest request, HttpServletResponse response) {

try {
if (!ExtensionContext.isPresent(FDK_SESSION)) {
throw new FdkSessionNotFound(
"Can not complete oauth process as session not found");
throw new FdkSessionNotFound("Can not complete oauth process as session not found");
}
Session fdkSession = ExtensionContext.get(FDK_SESSION, Session.class);
if (!fdkSession.getState()
Expand Down Expand Up @@ -181,8 +173,7 @@ public ResponseEntity<?> authorize(@RequestParam(value = "company_id") String co
sessionStorage.saveSession(fdkSession);

String compCookieName = FdkConstants.SESSION_COOKIE_NAME + "_" + fdkSession.getCompany_id();
ResponseCookie resCookie = ResponseCookie.from(compCookieName,
fdkSession.getId())
ResponseCookie resCookie = ResponseCookie.from(compCookieName, fdkSession.getId())
.httpOnly(true)
.sameSite("None")
.secure(true)
Expand All @@ -195,16 +186,14 @@ public ResponseEntity<?> authorize(@RequestParam(value = "company_id") String co
ExtensionContext.set(EXTENSION, ext);
ExtensionContext.set(COMPANY_ID, companyId);
ExtensionContext.set(APPLICATION_ID, applicationId);
if (Objects.nonNull(ext.getWebhookService()) &&
Objects.nonNull(ext.getExtensionProperties()
.getWebhook()) &&
Objects.nonNull(ext.getExtensionProperties()
.getWebhook()
.getSubscribe_on_install()) &&
if (Objects.nonNull(ext.getWebhookService()) && Objects.nonNull(ext.getExtensionProperties()
.getWebhook()) && Objects.nonNull(
ext.getExtensionProperties()
.getWebhook()
.getSubscribe_on_install()
.equals(Boolean.TRUE)) {
.getSubscribe_on_install()) && ext.getExtensionProperties()
.getWebhook()
.getSubscribe_on_install()
.equals(Boolean.TRUE)) {
PlatformClient platformClient = ext.getPlatformClient(companyId, token);
ext.getWebhookService()
.syncEvents(platformClient, null);
Expand All @@ -214,10 +203,11 @@ public ResponseEntity<?> authorize(@RequestParam(value = "company_id") String co
.getAuth()
.apply(ExtensionContext.get());

return ResponseEntity.status(HttpStatus.PERMANENT_REDIRECT)
return ResponseEntity.status(HttpStatus.TEMPORARY_REDIRECT)
.header(Fields.X_COMPANY_ID, fdkSession.getCompany_id())
.header(HttpHeaders.LOCATION, redirectUrl)
.header(HttpHeaders.SET_COOKIE, resCookie.toString())
// .cacheControl(CacheControl.noCache())
.build();
} catch (Exception error) {
log.error("Exception in auth call ", error);
Expand All @@ -228,15 +218,13 @@ public ResponseEntity<?> authorize(@RequestParam(value = "company_id") String co
}

@PostMapping(path = "/uninstall")
public ResponseEntity<?> uninstall(@RequestBody Client client,
HttpServletRequest request,
HttpServletResponse response
) {
public ResponseEntity<?> uninstall(@RequestBody Client client, HttpServletRequest request,
HttpServletResponse response) {
try {
if (!ext.isOnlineAccessMode()) {
String sid = Session.generateSessionId(false,
new Option(client.getCompany_id(), ext.getExtensionProperties()
.getCluster()));
String sid = Session.generateSessionId(false, new Option(client.getCompany_id(),
ext.getExtensionProperties()
.getCluster()));
Session fdkSession = sessionStorage.getSession(sid);
AccessToken rawToken = new AccessToken();
rawToken.setExpiresIn(fdkSession.getExpires_in());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fynd.extension.session.Session;
import com.fynd.extension.session.SessionStorage;
import com.fynd.extension.utils.ExtensionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
Expand All @@ -23,6 +24,7 @@
import static com.fynd.extension.utils.ExtensionContext.Keys.FDK_SESSION;

@Component
@Slf4j
public class SessionInterceptor implements HandlerInterceptor {

@Autowired
Expand Down

0 comments on commit 8a72ce1

Please sign in to comment.