natf17 / shopify-embedded-app

Enables any web app using Spring Security to operate as a Shopify embedded app.
23 stars 9 forks source link
shopify shopify-embedded-applications spring spring-boot spring-boot-2 spring-security

This project replaces the shopify-spring-boot-embedded-app project

This application enables any Spring web application with Spring Security to become a Shopify app and use Shopify's default OAuth offline access token.

Running the App

If you're using the Spring Boot security starter, this translates to version 2.2.X.

Obtaining Information for Your Shopify App

Once you have a development store, create a private app.

  1. Fill out "App name" with the name of your choice.
  2. Add your "App URL":
  3. For "Whitelisted redirection URL(s)" add:

Now that you've created your app, you're given an API key and an API key secret.

  1. Copy the API key and API key secret from the Shopify site.
  2. Store them, along with the desired scope, in a .properties file.
ppublica.shopify.security.client.client_id=your-key
ppublica.shopify.security.client.client_secret=your-key-secret
ppublica.shopify.security.client.scope=scope1,scope2,...
  1. Choose the password that the Spring encryptors will use to encrypt the token and add it to your .properties file:
ppublica.shopify.security.cipher.password=your-password

Adding the project

If you're using Maven, add the following under the <dependencies> element in the pom.xml:

<dependency>
   <groupId>com.ppublica.shopify</groupId>
   <artifactId>shopify-embedded-app</artifactId>
   <version>1.0.0-RELEASE</version>
   <scope>compile</scope>
</dependency>

Preparing your Application

  1. Make sure your Spring/Spring Boot application can find the security beans in the jar.

    @ComponentScan(basePackages = {"com.ppublica.shopify.security"})
  2. Make sure the following beans are in the ApplicationContext:

    • MappingJackson2HttpMessageConverter
    • JdbcTemplate
  3. Add the following to your WebSecurityConfigurerAdapter:

    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated().and()
            .requiresChannel().and()
            .oauth2Login();
    }
    }
  4. Your database is expected to have the following schema:

    |---------------------------STOREACCESSTOKENS-------------------------------|
    |                                                                           |
    |id--storeDomain--tokenType--tokenValue--salt--issuedAt--expiresAt--scopes--|
    |                                                                           |
    |---------------------------------------------------------------------------|
  5. Make sure you use HTTPS to comply with Shopify's security requirements.

  6. Make sure your app is running and is live at the hostname you specified.

Result

The following endpoints were registered:

/install/shopify?shop={your-store-name.myshopify.com}:

/init:

/login/app/oauth2/code/**:

/info:

/logout:

Customize the default paths

Coming soon!